我遵循本教程,但我遇到了一些麻烦。 https://www.youtube.com/watch?v=3tf8XlhsQAo
"""mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^webapp/', include("webapp.urls")),
url(r'^$',include("personal.urls")),
]
我已经编写了上面的代码,但是当我执行时我收到以下错误,是否有人可以帮助我理解此错误消息的含义?
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\USER\Desktop\Django\mysite\mysite\urls.py", line 24, in <modu
>
url(r'^$',include("personal.urls")),
File "C:\Users\USER\AppData\Local\Programs\Python\Python36-32\lib\site-packa
s\django\conf\urls\__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\USER\AppData\Local\Programs\Python\Python36-32\lib\importlib\
init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\USER\Desktop\Django\mysite\personal\urls.py", line 5, in <mod
e>
url(r'^admin/',admin.site.urls),
NameError: name 'admin' is not defined
答案 0 :(得分:3)
回溯告诉您personal\urls.py
文件中发生了错误:
File "C:\Users\USER\Desktop\Django\mysite\personal\urls.py", line 5, in <mode>
url(r'^admin/',admin.site.urls),
您已在mysite\urls.py
中添加了管理网址,
您不应在personal\urls.py
中包含管理员网址。从personal\urls.py
删除该行。
答案 1 :(得分:1)
您导入管理员时粘贴的代码没有问题。
您的个人/ urls.py中的代码
url(r'^admin/',admin.site.urls),
注意代码和回溯之间的空间差异。
你在app的urls.py上不需要那一行。只需删除它并试一试。
答案 2 :(得分:0)
文件“C:\ Users \ USER \ Desktop \ Django \ mysite \ personal \ urls.py”,第5行,在 URL(R '^管理员/',admin.site.urls) NameError:名称'admin'未定义
任何人都可以帮我理解这条错误信息的含义吗?
是:它告诉你在文件C的第5行:\ Users \ USER \ Desktop \ Django \ mysite \ personal \ urls.py - 这是tee -a
- 你正在引用变量(“name” “)url(r'^admin/',admin.site.urls),
,此文件中此时尚未定义。 IOW,你正试图使用一个不存在的变量。
正如Alasdaïr和Rahul已经提到的那样,因为你已经在你的根urlconfig(这是标准设置)中包含了admin
,所以你不需要它,所以明显的解决方案就是删除这一行 - 特别是因为你将admin.urls
包含在你网站的顶层,所以两个配置都会指向相同的网址。
但是如果你想在personal.urls
出于任何其他原因使用admin
模块 - 或者从personal.urls
而不是你的根urlconfig包含它 - 你需要明确导入personal.urls
顶部的admin
模块 - Python的“导入”与PHP的“包含”完全不同,并且不会神奇地为所有项目的模块提供名称。