我正在做一个Python-Django网络应用。我在urls.py上遇到错误
我正在尝试使用python manage.py runserver
运行项目
下面给出的是我的错误。
Not Found: /hello
[09/Nov/2016 16:22:34] "GET /hello HTTP/1.1" 404 1920
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0xb68f7924>
Traceback (most recent call last):
File "/home/nishanth/nenv/local/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/nishanth/nenv/local/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/home/nishanth/nenv/local/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/core/management/base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "/home/nishanth/nenv/local/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/core/management/base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "/home/nishanth/nenv/local/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/nishanth/nenv/local/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/home/nishanth/nenv/local/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/home/nishanth/nenv/local/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/nishanth/nenv/local/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/urls/resolvers.py", line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/nishanth/nenv/local/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/nishanth/nenv/local/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/urls/resolvers.py", line 306, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/nishanth/Documents/register/register/urls.py", line 16, in <module>
from django.conf.urls import patterns, include, url
ImportError: cannot import name patterns
Performing system checks...
这是我的urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns['',
url(r'^admin/', admin.site.urls),
url(r'^hello/$', 'blog.views.hello'),
url(r'^hello_template/$' , 'blog.views.hello_template'),
]
我该如何解决这个问题
答案 0 :(得分:5)
较新版本的Django(如您所使用的1.10)不使用patterns
。只需将网址放在列表中即可。您还需要导入将处理每个URL的视图函数,并将这些函数作为参数提供而不是字符串:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/$', views.hello),
url(r'^hello_template/$' , views.hello_template),
]
请参阅https://docs.djangoproject.com/en/1.10/topics/http/urls/#example