Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django URL模式设置_Python_Django - Fatal编程技术网

Python Django URL模式设置

Python Django URL模式设置,python,django,Python,Django,我有一个Django项目,工作的url.py如下所示: urlpatterns = [ path('', views.index, name='index'), path('polls/search', views.search, name='search'), ] 然后我想在url.py中为我的图像添加额外的路径 urlpatterns += patterns('django.views.static',(r'^media/(?P<path>.*)','serv

我有一个Django项目,工作的
url.py
如下所示:

urlpatterns = [
    path('', views.index, name='index'),
    path('polls/search', views.search, name='search'),

]
然后我想在url.py中为我的图像添加额外的路径

urlpatterns += patterns('django.views.static',(r'^media/(?P<path>.*)','serve',{'document_root':settings.MEDIA_ROOT}), )

我使用的是python 3.4和Django 2.0.8。如何正确地将附加路径添加到原始URL.py?谢谢

看起来使用
模式将不再有效。由于您正在尝试为静态文件提供服务,请尝试以下操作:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
并在settings.py中设置媒体URL和媒体根目录

为了使其在模板中工作,您可以执行以下操作:

{% load static %}
<body data-media-url="{% get_media_prefix %}">
{%load static%}

来自django.conf.url导入模式
明白了吗?不起作用。错误:导入错误:无法导入名称“模式”。请更正我的错误,您正在尝试将媒体链接添加到URL,对吗?谢谢。但图像仍然没有显示:[22/Aug/2018 21:55:55]“POST/polls/polls/search HTTP/1.1”200 852未找到:/polls/polls/{MEDIA_URL}my_img.jpg[22/Aug/2018 21:55:55]“GET/polls/polls/%7B%7B MEDIA_URL%7D%7Dmy_img.jpg HTTP/1.1”4042582是的,URL显示MEDIA_URL没有解析;我将编辑这个问题
{% load static %}
<body data-media-url="{% get_media_prefix %}">