尝试SubURL

时间:2018-04-30 10:31:16

标签: django python-3.x

我不仅是Django的新手,也是Python的新手。我正在使用我母语的视频教程;但是在应用说明时,我收到了以下错误:

> Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f6b2477eae8>
Traceback (most recent call last):
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/core/management/base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/urls/resolvers.py", line 397, in check
    for pattern in self.url_patterns:
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/urls/resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/urls/resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, 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 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/stockfish/Masaüstü/DjangoOne/DjangoKod/urls.py", line 25, in <module>
    re_path(r'^post/', include('post.urls')),
  File "/home/stockfish/Masaüstü/DjangoOne/myvenv/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, 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 674, in exec_module
  File "<frozen importlib._bootstrap_external>", line 781, in get_code
  File "<frozen importlib._bootstrap_external>", line 741, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/stockfish/Masaüstü/DjangoOne/post/urls.py", line 6
    re_path(r'^detail/$', PostDetail)
          ^
SyntaxError: invalid syntax

我应该注意,视频教程是在以前版本的Django中制作的(据我记得DjangoVer = 1.9);但是我使用的是Python和Django的最新版本。

例如:在其中一个视频中,教师使用的内容如下:

url(r'^$', HomeView)

但这对我的版本来说并不正确。我做了一些研究,并注意到我应该使用&#39; re_path&#39;而不是&#39; url&#39;对于较新版本的Django并更正了它。 (右?)

作为结论:

1)如何修复上一个错误? 2)如何为Django提供简单有效的指导? Django的主要网站看起来有点复杂。(每当我遇到困难时,让人们在这里忙碌真的很遗憾。)

提前致谢!

1 个答案:

答案 0 :(得分:0)

在urls.py中使用此功能。 urlpatterns

中缺少逗号

<强>实施例

from django.urls import include, path, re_path
from post.views import *
urlpatterns = [
    re_path(r'^index/$', PostIndex),
    re_path(r'^detail/$', PostDetail),
    re_path(r'^create/$', PostCreate), 
    re_path(r'^update/$', PostUpdate),
    re_path(r'^delete/$', PostDelete)
]