【问题标题】:Syntax Error in urls.py path upgrading to Django 2.1升级到 Django 2.1 的 urls.py 路径中的语法错误
【发布时间】:2018-09-06 14:59:54
【问题描述】:

在手动升级到 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

【问题讨论】:

  • 看起来像间距问题。如果你把它写成] +static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)(全部在 one 行,带有 no 缩进)
  • @WillemVanOnsem 谢谢。但不幸的是问题仍然存在。
  • 在您的path('logout', auth_view... 中,您忘记关闭括号了。

标签: django url


【解决方案1】:

您忘记在logout url 上加上括号:

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)

【讨论】:

  • 谢谢,成功了!我应该使用 pycharm ,正在使用 NotePad++。
猜你喜欢
  • 1970-01-01
  • 2018-11-04
  • 2014-10-20
  • 1970-01-01
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-26
相关资源
最近更新 更多