当手动升级到Django 2.1时,urls.py中出现语法错误。我已经删除了正则表达式字符,但错误仍然存在。怎么了?我检查了自己是否没有错过,
或花括号,但仍然不知道是什么导致了错误。
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from django.views.generic.base import RedirectView
from django.conf import settings
from django.conf.urls.static import static
import app.forms
import app.views
from django.urls import include,path
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
path('', app.views.gallery, name='gallery'),
path('favicon\.ico', RedirectView.as_view(url='/static/icons/favicon.ico', permanent=True)),
path('<slug:album_slug>', app.views.AlbumDetail.as_view(), name='album'), #app.views.AlbumView.as_view()
# Auth related urls
path('accounts/login/', auth_views.LoginView.as_view(template_name='registration\login.html'), name='login'),
path('logout', auth_views.LogOutView.as_view(next_page,template_name='registration\logged_out.html', name='logout'),
# Uncomment the next line to enable the admin:
path('admin/', admin.site.urls),
# Uncomment the admin/doc line below to enable admin documentation:
path('admin/doc/', include('django.contrib.admindocs.urls')),
]
+static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
handler404 = 'app.views.handler404'
错误跟踪:
(Django11) C:\Users\Kaleab\Desktop\Photo_Gallery\django-photo-gallery\django_pho
to_gallery>py -3.7 manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper
at 0x000000D918D0E268>
Traceback (most recent call last):
File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 225, in
wrapper
fn(*args, **kwargs)
File "C:\Python37\lib\site-packages\django\core\management\commands\runserver.
py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Python37\lib\site-packages\django\core\management\base.py", line 379,
in check
include_deployment_checks=include_deployment_checks,
File "C:\Python37\lib\site-packages\django\core\management\base.py", line 366,
in _run_checks
return checks.run_checks(**kwargs)
File "C:\Python37\lib\site-packages\django\core\checks\registry.py", line 71,
in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Python37\lib\site-packages\django\core\checks\urls.py", line 40, in c
heck_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "C:\Python37\lib\site-packages\django\core\checks\urls.py", line 57, in _
load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "C:\Python37\lib\site-packages\django\utils\functional.py", line 37, in _
_get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python37\lib\site-packages\django\urls\resolvers.py", line 533, in ur
l_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python37\lib\site-packages\django\utils\functional.py", line 37, in _
_get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python37\lib\site-packages\django\urls\resolvers.py", line 526, in ur
lconf_module
return import_module(self.urlconf_name)
File "C:\Python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 675, in exec_module
File "<frozen importlib._bootstrap_external>", line 782, in get_code
File "<frozen importlib._bootstrap_external>", line 742, in source_to_code
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Kaleab\Desktop\Photo_Gallery\django-photo-gallery\django_photo_
gallery\django_photo_gallery\urls.py", line 30
]
^
SyntaxError: invalid syntax
答案 0 :(得分:1)
您忘记关闭logout
网址上的括号:
path(
'logout',
auth_views.LogOutView.as_view(next_page,template_name='registration\logged_out.html'),
# was missing ^
name='logout'
),
因此,您可以使用以下方法修复urls.py
:
urlpatterns = [
path('', app.views.gallery, name='gallery'),
path(
'favicon\.ico',
RedirectView.as_view(url='/static/icons/favicon.ico', permanent=True)
),
path(
'<slug:album_slug>',
app.views.AlbumDetail.as_view(),
name='album'
), #app.views.AlbumView.as_view()
# Auth related urls
path(
'accounts/login/',
auth_views.LoginView.as_view(template_name='registration\login.html'),
name='login'
),
path(
'logout',
auth_views.LogOutView.as_view(next_page,template_name='registration\logged_out.html'),
name='logout'
),
# Uncomment the next line to enable the admin:
path('admin/', admin.site.urls),
# Uncomment the admin/doc line below to enable admin documentation:
path('admin/doc/', include('django.contrib.admindocs.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)