Edamame
Edamame

Reputation: 25366

Django urlpatterns settings

I have a Django project, the working urls.py looks like:

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

]

Then I want to add additional path for my image in the urls.py

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

But I got:

 unresolved reference 'patterns'

I am using python 3.4 and Django 2.0.8. How do I properly add the additional path to my original urls.py ? Thanks!

Upvotes: 1

Views: 768

Answers (1)

Athena
Athena

Reputation: 3228

It looks like using patterns won't work anymore. Since you're trying to serve static files, try this:

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)

And set MEDIA_URL and MEDIA_ROOT in your settings.py.

In order to get it to work in your templates, you'd do something like this:

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

Django docs

Upvotes: 1

Related Questions