AttributeError:模块'django.contrib.auth.views'没有属性'login'

时间:2017-10-27 12:59:36

标签: python django django-rest-framework

我的django休息框架出错,我在Windows 10操作系统上运行它。这是整个错误:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "c:\django\django\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "c:\django\django\django\core\management\__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "c:\django\django\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "c:\django\django\django\core\management\base.py", line 332, in execute
    self.check()
  File "c:\django\django\django\core\management\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "c:\django\django\django\core\management\commands\migrate.py", line 58, in _run_checks
    issues.extend(super()._run_checks(**kwargs))
  File "c:\django\django\django\core\management\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "c:\django\django\django\core\checks\registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "c:\django\django\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "c:\django\django\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "c:\django\django\django\urls\resolvers.py", line 385, in check
    for pattern in self.url_patterns:
  File "c:\django\django\django\utils\functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "c:\django\django\django\urls\resolvers.py", line 524, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "c:\django\django\django\utils\functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "c:\django\django\django\urls\resolvers.py", line 517, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\django\travbudserver\travbudserver\urls.py", line 41, in <module>
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
  File "c:\django\django\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\urls.py", line 24, in <module>
    url(r'^login/$', views.login, template_name, name='login'),
AttributeError: module 'django.contrib.auth.views' has no attribute 'login'

这是我的url.py

from django.conf.urls import include, url
from django.contrib import admin
from django.urls import path
from rest_framework import routers, serializers, viewsets
from polls.models import Question, Choice

# Serializers define the API representation.
class QuestionSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Question
        fields = ('question_text', 'pub_date')

# ViewSets define the view behavior.
class QuestionViewSet(viewsets.ModelViewSet):
    queryset = Question.objects.all()
    serializer_class = QuestionSerializer

# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'question', QuestionViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

有没有人对我的情况有所了解?提前谢谢......

2 个答案:

答案 0 :(得分:7)

您目前将登录网址定义为:

url(r'^login/$', views.login, template_name, name='login'),

在Django 2.1中,这是deprecated functionality was removed

要解决此问题,您可以将行更改为:

url(r'^login/$', views.LoginView.as_view(template_name=template_name), name='login'),

有关详细信息,请参阅authentication views documentation

答案 1 :(得分:1)

您正在使用Django的未发布的主分支,它删除了旧的基于功能的登录视图。您应该使用已发布的版本1.11,它们仍然存在(但已弃用)。

最新的DRF大师已删除此引用,但无论如何你应该坚持发布版本。