문제

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,
    }),
도움이 되었습니까?

해결책

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, }),
)

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top