Frage

I add urls in these lines for media and image output to the template. But I meet such a bug. name 'settings' is not defined How do I fix it?

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

Lösung

Add

from django.conf import settings

to the top of your file. And change the operator used in urlpatterns variable assignment.

urlpatterns =+ patterns('',

should be

urlpatterns += patterns('',

There is no =+ operator in python.

EDIT:

From the urlpattern posted in comment, I see that there is no other urlpattern and the urlpattern should be as follows without the + sign.

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

Andere Tipps

Add

from django.conf import settings

To the top of your file.

for anyone struggling with this situation first thing make sure you have

from django.conf import settings

in your urls.py file

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top