Python: 'module' object has no attribute 'logout_view'
我目前正在尝试实现注销到我的 django 站点的功能,但在虚拟环境中尝试使用命令 python manage.py runserver 时出现以下错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | (ll_env) C:\\Users\\me\\Desktop\\learning_log>python manage.py runserver Performing system checks... Unhandled exception in thread started by <function wrapper at 0x0000000006693BA8> Traceback (most recent call last): File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\utils\\autoreload.py", line 228, in wrapper fn(*args, **kwargs) File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\core\\management\\commands\ unserver.py", line 125, in inner_run self.check(display_num_errors=True) File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\core\\management\\base.py", line 359, in check include_deployment_checks=include_deployment_checks, File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\core\\management\\base.py", line 346, in _run_checks return checks.run_checks(**kwargs) File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\core\\checks\ egistry.py", line 81, in run_checks new_errors = check(app_configs=app_configs) File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\core\\checks\\urls.py", line 16, in check_url_config return check_resolver(resolver) File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\core\\checks\\urls.py", line 26, in check_resolver return check_method() File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\urls\ esolvers.py", line 254, in check for pattern in self.url_patterns: File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\utils\\functional.py", line 35, in __get__ res = instance.__dict__[self.name] = self.func(instance) File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\urls\ esolvers.py", line 405, in url_patterns patterns = getattr(self.urlconf_module,"urlpatterns", self.urlconf_module) File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\utils\\functional.py", line 35, in __get__ res = instance.__dict__[self.name] = self.func(instance) File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\urls\ esolvers.py", line 398, in urlconf_module return import_module(self.urlconf_name) File"c:\\users\\me\\python27\\Lib\\importlib\\__init__.py", line 37, in import_module __import__(name) File"C:\\Users\\me\\Desktop\\learning_log\\learning_log\\urls.py", line 22, in <module> url(r'^users/', include('users.urls', namespace='users')), File"C:\\Users\\me\\Desktop\\learning_log\\ll_env\\lib\\site-packages\\django\\conf\\urls\\__init__.py", line 50, in include urlconf_module = import_module(urlconf_module) File"c:\\users\\me\\python27\\Lib\\importlib\\__init__.py", line 37, in import_module __import__(name) File"C:\\Users\\me\\Desktop\\learning_log\\users\\urls.py", line 13, in <module> url(r'^logout/$', views.logout_view, name='logout'), AttributeError: 'module' object has no attribute 'logout_view' |
这是我在 learning_log\\\\users 目录中的 urls.py 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from django.conf.urls import url from django.contrib.auth.views import login from . import views urlpatterns = [ # Login page url(r'^login/$', login, {'template_name': 'users/login.html'}, name='login'), # Logout page url(r'^logout/$', views.logout_view, name='logout'), # Registration page url(r'^register/$', views.register, name='register'), ] |
对上述文件的最新补充是:
1 2 | # Logout page url(r'^logout/$', views.logout_view, name='logout'), |
以下代码作为最新添加添加到了我的 views.py 文件的末尾:
1 2 3 4 5 6 7 8 | from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse from django.contrib.auth import logout def logout_view(request): """Log the user out.""" logout(request) return HttpResponseRedirect(reverse('learning_logs:index')) |
这是我的 base.html 文件:
1 2 3 4 5 6 7 8 9 10 11 12 | <p><center>[wp_ad_camp_2]</center></p><p> Learning Log - Topics - {% if user.is_authenticated %} Hello, {{ user.username }}. log out {% else %} log in {% endif %} </p> {% block content %}{% endblock content %} |
这是对我的 base.html 文件的最新添加:
1 | log out |
在上述添加之前,代码运行良好。知道我这可能很容易解决,但是有人有解决这个问题的方法吗?我目前正在使用 Django 1.11.7 和 Python 2.7.14。
当您在
中使用相对导入时
1 | from . import views |
这相当于
1 | from users import views |
但是,听起来您的
1 | from learning_logs import views |
请注意,Django 有一个
1 2 3 | from django.contrib.auth.views import logout url(r'^logout/$', logout, name='logout'), |
然后在你的设置中设置
1 | LOGOUT_REDIRECT_URL = 'learning_logs:index' |