2016-11-09 112 views
0

我做了Python的Django的網絡app.And我當 我試圖運行下面給出的使用python manage.py runserver 項目上urls.py錯誤是我的錯誤。導入錯誤的urls.py在運行的Python Django應用程序

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'), 
] 

我怎樣才能解決這個問題的Django的

+0

如果這個代碼來自一個教程,那麼它是出的日期。上面的代碼是爲Django 1.7或更早的版本編寫的。如果你開始學習Django,那麼請尋找更新的教程,否則你會遇到更多這樣的問題。 – Alasdair

回答

5

較新版本(如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

+0

因爲字符串魔法也被刪除了 –

+0

@Alasdair我發佈了這個例子,希望他們能從那裏拿起。感謝您的注意。 –