升级到Django 2.1的urls.py路径中的语法错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了升级到Django 2.1的urls.py路径中的语法错误相关的知识,希望对你有一定的参考价值。

在手动升级到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='registrationlogin.html'), name='login'),
    path('logout', auth_views.LogOutView.as_view(next_page,template_name='registrationlogged_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:UsersKaleabDesktopPhoto_Gallerydjango-photo-gallerydjango_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:Python37libsite-packagesdjangoutilsautoreload.py", line 225, in
wrapper
    fn(*args, **kwargs)
  File "C:Python37libsite-packagesdjangocoremanagementcommands
unserver.
py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "C:Python37libsite-packagesdjangocoremanagementase.py", line 379,
 in check
    include_deployment_checks=include_deployment_checks,
  File "C:Python37libsite-packagesdjangocoremanagementase.py", line 366,
 in _run_checks
    return checks.run_checks(**kwargs)
  File "C:Python37libsite-packagesdjangocorechecks
egistry.py", line 71,
in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:Python37libsite-packagesdjangocorechecksurls.py", line 40, in c
heck_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "C:Python37libsite-packagesdjangocorechecksurls.py", line 57, in _
load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "C:Python37libsite-packagesdjangoutilsfunctional.py", line 37, in _
_get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:Python37libsite-packagesdjangourls
esolvers.py", line 533, in ur
l_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:Python37libsite-packagesdjangoutilsfunctional.py", line 37, in _
_get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:Python37libsite-packagesdjangourls
esolvers.py", line 526, in ur
lconf_module
    return import_module(self.urlconf_name)
  File "C:Python37libimportlib\__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:UsersKaleabDesktopPhoto_Gallerydjango-photo-gallerydjango_photo_
gallerydjango_photo_galleryurls.py", line 30
    ]
    ^
SyntaxError: invalid syntax
答案

你忘了关闭logout网址上的一个括号:

path(
    'logout',
    auth_views.LogOutView.as_view(next_page,template_name='registrationlogged_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='registrationlogin.html'),
        name='login'
    ),
    path(
        'logout',
        auth_views.LogOutView.as_view(next_page,template_name='registrationlogged_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)

以上是关于升级到Django 2.1的urls.py路径中的语法错误的主要内容,如果未能解决你的问题,请参考以下文章

Django 从 3.2 -> 4.0 和 DRF 路径更新

django 在 urls.py 中重定向

Django 路由

覆盖 Django 中的 brabeion 模板路径

django中urls.py中的include()是什么

django入门与实践 - 关于升级到django 3.7,三种模板超链接配置(编辑中)