Django {%url%}表现得很奇怪

时间:2013-04-08 03:49:40

标签: django

有人可以解释为什么会有效:

<li><a href="/index/">HOME</a></li>
<li><a href="/project/">PROJECTS</a></li>

虽然这不是:

 <li><a href="{% url projects.views.index %}">HOME</a></li>
 <li><a href="{% url projects.views.project %}">PROJECTS</a></li>

我在/ index / No模块名为project 的 ImportError得到500错误但是我不想在任何地方导入项目。 (我正在导入Project,它已经导入了好几天,并且当我不使用{%url%}样式链接时工作正常)。我在这里看到了很多答案,其他地方的博客帖子暗示{%url%}是一个坏主意。如果有必要,我很乐意停止使用它们,但是因为我的生活无法看到错误的来源。

urls.py的内容

from django.conf.urls import patterns, include, url
from django.conf import settings

from django.views.static import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^index/', 'projects.views.index', name='index'),
    url(r'^login/', 'project.views.login', name='login'),
    url(r'^project/(?P<project_id>\d+)/$', 'projects.views.project', name='project'),
    url(r'^project/', 'projects.views.project', name='project'),

    url(r'^sched/(?P<project_id>\d+)/$', 'projects.views.schedule'),
    url(r'^luminaires/(?P<project_id>\d+)/$', 'projects.views.luminaires'),
    url(r'^luminaire/(?P<project_id>\d+)/(?P<luminaire_id>\d+)/$', 'projects.views.luminaire'),
    url(r'^deleteproject/(?P<project_id>\d+)/$', 'projects.views.deleteproject', name = 'deleteproject'),


    url(r'^admin/', include(admin.site.urls)),
    # Examples:
    # url(r'^$', 'relume.views.home', name='home'),
    # url(r'^relume/', include('relume.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes':True}),
)

1 个答案:

答案 0 :(得分:4)

在查看了您的urls.py文件后,您似乎在登录的网址行中输入了一个拼写错误,其中您使用的是project而不是projects