Django 2.0及以上版本的admin无法显示图片问题
最近在自学Django,使用的是Django 2.0 + Python 3.6 的配置,在写一个上传下载图片应用时遇到了一个问题,就是写好数据库,业务和admin的基本配置时,能看到图片名称,图片却打不开。如图:
在这个界面点击图片的链接是Django提示404 not found的,即是找不到图片路径。
我的admin很常规:
from django.contrib import admin
# Register your models here.
from .models import ImgData
class ImgDataAdmin(admin.ModelAdmin):
list_display = ('name', 'user', 'create_at', 'image')
list_filter = ['user', 'create_at']
search_fields = ['name']
admin.site.register(ImgData, ImgDataAdmin)
后来我找了很多坛子里大神发的解决办法,也尝试了很多,包括在admin里写函数显示之类的。看来看去也就这一条是比较靠谱的,只需要修改url:
from django.conf.urls import url
from django.contrib import admin
from settings import MEDIA_ROOT
from django.views.static import serve
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT}),
]
其中我觉得管用的就是url(r’^media/(?P
urlpatterns = [
path('admin/', admin.site.urls),
]
于是我发动了一下小聪明,把原代码改成了path(‘media/(str:path.*)’, serve, {“document_root”: MEDIA_ROOT}),这样我的整个url代码就变成了:
from django.contrib import admin
from django.urls import path, include
from picsite.settings import MEDIA_ROOT
from django.views.static import serve
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', include('login.urls')),
path('updownload/', include('updownload.urls')),
path('data_form/', include('updownload.urls')),
path('media/(<str:path>.*)', serve, {"document_root": MEDIA_ROOT})
]
这里要注意和上面不同的点就是不能直接从settings里直接引用MEDIA_ROOT,你的项目.settings import MEDIA_ROOT 这样才能正常应用,比如我所写的from picsite.settings import MEDIA_ROOT 。
发现改完了之后并没有奏效,显示的依旧是:
之后我查了很多方法都没有办法解决,老师就说让我去看Django的官方文档,于是就开始啃英语…不过幸运的是,就看了半个小时就看到了我想要的:
URL调度器
Using regular expressions¶
If the paths and converters syntax isn’t sufficient for defining your URL patterns, you can also use regular expressions. To do so, use re_path() instead of path().
In Python regular expressions, the syntax for named regular expression groups is (?Ppattern), where name is the name of the group and pattern is some pattern to match.
Here’s the example URLconf from earlier, rewritten using regular expressions:
from django.urls import path, re_path
from . import views
urlpatterns = [
path(‘articles/2003/’, views.special_case_2003),
re_path(r’^articles/(?P[0-9]{4})/′,views.yeararchive),repath(r′articles/(?P[0−9]4)/(?P[0−9]2)/′,views.yeararchive),repath(r′articles/(?P[0−9]4)/(?P[0−9]2)/’, views.month_archive),
re_path(r’^articles/(?P[0-9]{4})/(?P[0-9]{2})/(?P[\w-]+)/$’, views.article_detail),
]
This accomplishes roughly the same thing as the previous example, except:
The exact URLs that will match are slightly more constrained. For example, the year 10000 will no longer match since the year integers are constrained to be exactly four digits long.
Each captured argument is sent to the view as a string, regardless of what sort of match the regular expression makes.
When switching from using path() to re_path() or vice versa, it’s particularly important to be aware that the type of the view arguments may change, and so you may need to adapt your views.
于是我把最后一行改成了re_path(r’^media/(?P
from django.contrib import admin
from django.urls import path, include, re_path
from picsite.settings import MEDIA_ROOT
from django.views.static import serve
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', include('login.urls')),
path('updownload/', include('updownload.urls')),
path('data_form/', include('updownload.urls')),
re_path(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT}),
#path('media/(<str:path>.*)', serve, {"document_root": MEDIA_ROOT})
]
这样图片就可以正常访问了。
使用此方法显示图片的前提当然是settings里的media要设置好。
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')
MEDIA_URL = '/media/'
re_path这个好像是1.6版本后对原url语法的代替方法,不知道是不是火星了,与同是小白的初学者们分享一下吧。Django这么频繁的改来改去,以前好多语法都要改动,这样很容易失去新手coder啊:(
引用部分源:Django meida(admin后台上传图片并可访问)
作者:楚黠王
来源:CSDN
原文:https://blog.csdn.net/taopeihan511/article/details/81668769
版权声明:本文为博主原创文章,转载请附上博文链接!