我刚刚设置了virtualenv,然后在一个新项目" devp1"下创建了一个名为" app1"的新应用程序。我已按以下方式编辑了文件:
在" app1"中的views.py DIR
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse("wow my first page")
settings.py in" devp1" DIR
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
]
url.py in" devp1" DIR
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'','app1.views.home'),
]
然而,当我运行服务器时,我收到以下错误,我还没有找到为什么它无法看到" app1"。
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f7ab830b400>
Traceback (most recent call last):
File "/home/usr1/dev/lib/python3.4/site-packages/django/utils/autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "/home/usr1/dev/lib/python3.4/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
File "/home/usr1/dev/lib/python3.4/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/home/usr1/dev/lib/python3.4/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/home/usr1/dev/lib/python3.4/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/usr1/dev/lib/python3.4/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/home/usr1/dev/lib/python3.4/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/home/usr1/dev/lib/python3.4/site-packages/django/urls/resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "/home/usr1/dev/lib/python3.4/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/usr1/dev/lib/python3.4/site-packages/django/urls/resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/usr1/dev/lib/python3.4/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/usr1/dev/lib/python3.4/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "/home/usr1/dev/lib64/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/usr1/dev/devp1/devp1/urls.py", line 22, in <module>
url(r'', app1.views.home),
NameError: name 'app1' is not defined
答案 0 :(得分:2)
看起来您的视图中没有引号。追溯显示
url(r'', app1.views.home),
但你的urls.py文件显示
url(r'','app1.views.home'),
如果没有这些引号,它将无法在您的应用程序中找到该视图。我也不认为你的正则表达式会按照你想要的方式工作。我会尝试这样做:
from app1 import views as app1_views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', app1_views.home, name='home'),
]