如何为settings.py创建urlpatterns和模式?

时间:2014-07-28 15:03:58

标签: python django

要访问静态,我需要在setting.py

中使用此代码
DEBUG = True
TEMPLATE_DEBUG = DEBUG

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

但是,urlpatterns在setting.py中不可用。它返回NameError: name 'urlpatterns' is not definedurlpatterns位于urls.py

"""THIS IS URLS.PY"""

from django.conf.urls import patterns, url
from rango import views

urlpatterns = patterns('',
        url(r'^$', views.index, name='index'),url(r'^rango/', views.about, name='about'))

如何urlpatterns

使setting.py可用

dirrectory: http://oi58.tinypic.com/s2unnt.jpg

谢谢!

2 个答案:

答案 0 :(得分:3)

你为什么不反其道而行之?将所有url放在urls.py中似乎更符合逻辑。将此添加到urls.py:

from django.conf import settings

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

答案 1 :(得分:1)

不,你已经理解错了。该URL代码属于urls.py,而不属于settings.py。您将设置导入到网址中,而不是相反。