2016-11-03 67 views
1

這是我的URL模式:如何修復django 1.10.2的url模式?

from django.conf.urls import url 
from django.contrib import admin 
from school.views import home 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    #url(r'^$', home, name = 'home') #school.views.home') 

    #url(r'^$', 'main.views.home', name = 'school_home') 
    url(r'^$', home, name = 'home') 
] 

urlpatterns = [ 
    #'django.contrib.auth.views', 

    url(r'^login/$', 'login', {'template_name': 'login.html'}, name = 'school_login'), 

    url(r'^logout/$', 'logout', {'next_page': 'school_home'}, name = 'school_logout'), 
    ] 

出於某種原因,我不斷收到這些可怕的錯誤,只是不會讓我的應用程序運行時,我問它的runserver。

這裏是我的追蹤錯誤消息:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper 
at 0x03F292B8> 
Traceback (most recent call last): 
    File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 226, in 
wrapper 
    fn(*args, **kwargs) 
    File "C:\Python34\lib\site-packages\django\core\management\commands\runserver. 
py", line 121, in inner_run 
    self.check(display_num_errors=True) 
    File "C:\Python34\lib\site-packages\django\core\management\base.py", line 374, 
in check 
    include_deployment_checks=include_deployment_checks, 
    File "C:\Python34\lib\site-packages\django\core\management\base.py", line 361, 
in _run_checks 
    return checks.run_checks(**kwargs) 
    File "C:\Python34\lib\site-packages\django\core\checks\registry.py", line 81, 
in run_checks 
    new_errors = check(app_configs=app_configs) 
    File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 14, in c 
heck_url_config 
    return check_resolver(resolver) 
    File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 24, in c 
heck_resolver 
    for pattern in resolver.url_patterns: 
    File "C:\Python34\lib\site-packages\django\utils\functional.py", line 35, in _ 
_get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "C:\Python34\lib\site-packages\django\urls\resolvers.py", line 313, in ur 
l_patterns 
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
    File "C:\Python34\lib\site-packages\django\utils\functional.py", line 35, in _ 
_get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "C:\Python34\lib\site-packages\django\urls\resolvers.py", line 306, in ur 
lconf_module 
    return import_module(self.urlconf_name) 
    File "C:\Python34\lib\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 "C:\Python34\Scripts\schoolDatabase\schoolDatabase\urls.py", line 31, in 
<module> 
    url(r'^login/$', 'login', {'template_name': 'login.html'}, name = 'school_lo 
gin'), 
    File "C:\Python34\lib\site-packages\django\conf\urls\__init__.py", line 85, in 
url 
    raise TypeError('view must be a callable or a list/tuple in the case of incl 
ude().') 
TypeError: view must be a callable or a list/tuple in the case of include(). 

我看了一下關於URL模式計算器等問題,我已經嘗試過調整,但它仍然無法工作。

任何人都可以幫忙嗎?

+0

這非常類似於[這個問題](http://stackoverflow.com/questions/38744285/django-urls-error-view-must-be-a-callable-or-a-list-元組中的元組?noredirect = 1&lq = 1),但我已經在下面回答了,以防您已經看到並且仍然卡住。 – Alasdair

+0

什麼鏈接?我沒看到它。你可以添加評論嗎? –

回答

0

導入auth視圖,然後更改您的URL模式以使用可調用。

from django.contrib.auth import views as auth_views 

urlpatterns = [ 
    url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name = 'school_login'), 
    url(r'^logout/$', auth_views.logout, {'next_page': 'school_home'}, name = 'school_logout'), 
]