Django 2.0及以上版本的admin无法显示图片问题

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 .*)$’, serve, {“document_root”: MEDIA_ROOT}这一行。于是我把最后一行加到了根url中,提示url无法被import,这个其实是Django学习者经常遇到的问题,因为Django 1.6以后的版本都没法使用url(…)了,用的都是path(…),比如:

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[09]4)/(?P[09]2)/,views.yeararchive),repath(r′articles/(?P[09]4)/(?P[09]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 .*)$’, serve, {“document_root”: MEDIA_ROOT}), 再引用了一下re_path于是我整个代码变成了:

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
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值