Django - URL文件中的SyntaxError

时间:2015-11-10 16:25:47

标签: python django django-urls

我的基本URL中出现了Django(Django 1.8,Python 3.4)的语法错误。这是ir:

的代码
from django.conf.urls import include, url
from django.contrib import admin

from login_vacations import views as login_views

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^login/$', login_views.index, name="login_vacations"),
    url(r'^logout/$', login_views.logout_vacations, name='logout_vacations'),
    url(r'^vacations/', include('vacations_app.urls')),
]

vacations_app存在于该文件夹中,并且其中有一个urls.py文件。错误如下:

invalid syntax (urls.py, line 10)

相反,如果我从include函数中删除单引号,则错误将为:

name 'vacations_app' is not defined

如果我完全删除第10行,应用程序将正常工作。但我不明白为什么。此行上的结构与第7行(包含管理员网址的那个)的结构完全相同。

vacations_app / urls.py包含以下代码:

from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView

from vacations_app import views as vacations_views

urlpatterns = [
    url(r'^$', vacations_views.vacations_public_view, name='vacations_public_view'),
    url(r'^my_vacations/$', vacations_views.my_vacations, name='my_vacations'),
    url(r'^new_vacation/$', vacations_views.new_vacation, name='new_vacation'),
    url(r'^show/(?P<vacation_id>\w+)/$'), vacations_views.show_vacation, name='show_vacation'),
    url(r'^show/(?P<vacation_id>\w+)/add_destination/$'), vacations_views.add_destination_to_vacation, name='add_destination_to_vacation'),
    ]

可以在此处找到错误的完整描述:http://dpaste.com/2A1B6DS

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:3)

你的vacations_app / urls.py在第10行和第11行看起来有两个额外的括号。现在你有:

url(r'^show/(?P<vacation_id>\w+)/$'), vacations_views.show_vacation, name='show_vacation'),
url(r'^show/(?P<vacation_id>\w+)/add_destination/$'), vacations_views.add_destination_to_vacation, name='add_destination_to_vacation'),

他们应该看起来像:

url(r'^show/(?P<vacation_id>\w+)/$', vacations_views.show_vacation, name='show_vacation'),
url(r'^show/(?P<vacation_id>\w+)/add_destination/$', vacations_views.add_destination_to_vacation, name='add_destination_to_vacation'),

答案 1 :(得分:-2)

您是否已将INSTALLED_APPS中的应用程序(vacations_app)添加到settings.py?我对你的urlpatterns = []也有点怀疑,因为我对我的某些项目有urlpatterns = patterns()

如果可能的话,你可以分享你的settings.py(遗漏了可识别的信息吗?)