Search code examples
pythondjangodjango-settings

name 'settings' is not defined


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

Solution

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