我有ImageUploadField
我将图像保存在my_project/forum_attachments
目录。
但是,当我尝试显示它们并通过此链接查看时:http://127.0.0.1:8000/forum_attachments/1466786056166112161_Nrns2WL.jpg 我收到错误
找不到页面(404)请求方法:GET请求 网址:http://127.0.0.1:8000/forum_attachments/1466786056166112161_Nrns2WL.jpg
我该怎么办?
UPD:urls.py
:
urlpatterns = [
url(r'^polls/', include('some_app.urls')),
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^about/', views.about, name='about'),
url(r'^login/$', auth_views.login, name='login'),
url(r'^user_logout/$', views.user_logout, name='user_logout'),
url(r'^index_old/', views.index_old, name='index_old'),
url(r'^forum/', views.forum, name='forum'),
url(r'^vip/', views.vip, name='vip'),
url(r'^test/', views.test, name='test'),
url(r'^forum_new/', views.forum_new, name='forum_new'),
]
答案 0 :(得分:0)
首先在settings.py
中设置:
MEDIA_URL = '/forum_attachments/'
然后在您的主urls.py
中添加以下代码:
from django.conf.urls.static import static
from django.conf import settings
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
答案 1 :(得分:0)
如果您未设置MEDIA_URL&设置或本地设置文件中的MEDIA ROOT,您应该按照以下步骤操作。
Settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(PROJECT_PATH,'../media/')
MEDIA_URL = '/media/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
STATIC_URL = '/static/'
现在在你的Urls.py中,执行以下操作:
from django.conf.urls.static import static
urlpatterns = [
url(r'^polls/', include('some_app.urls')),
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^about/', views.about, name='about'),
url(r'^login/$', auth_views.login, name='login'),
url(r'^user_logout/$', views.user_logout, name='user_logout'),
url(r'^index_old/', views.index_old, name='index_old'),
url(r'^forum/', views.forum, name='forum'),
url(r'^vip/', views.vip, name='vip'),
url(r'^test/', views.test, name='test'),
url(r'^forum_new/', views.forum_new, name='forum_new'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
'静态'已附加到urlpatterns,因为您的DEBUG可能在settings.py中设置为True。