Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 views
in Technique[技术] by (71.8m points)

python - AttributeError: module 'django.contrib.auth.views' has no attribute 'login'

I got error on my django rest framework, I am running it on windows 10 OS. this is the entire error:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "c:djangodjangodjangocoremanagement\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "c:djangodjangodjangocoremanagement\__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "c:djangodjangodjangocoremanagementase.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "c:djangodjangodjangocoremanagementase.py", line 332, in execute
    self.check()
  File "c:djangodjangodjangocoremanagementase.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "c:djangodjangodjangocoremanagementcommandsmigrate.py", line 58, in _run_checks
    issues.extend(super()._run_checks(**kwargs))
  File "c:djangodjangodjangocoremanagementase.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "c:djangodjangodjangocorechecks
egistry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "c:djangodjangodjangocorechecksurls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "c:djangodjangodjangocorechecksurls.py", line 23, in check_resolver
    return check_method()
  File "c:djangodjangodjangourls
esolvers.py", line 385, in check
    for pattern in self.url_patterns:
  File "c:djangodjangodjangoutilsfunctional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "c:djangodjangodjangourls
esolvers.py", line 524, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "c:djangodjangodjangoutilsfunctional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "c:djangodjangodjangourls
esolvers.py", line 517, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:UsersuserAppDataLocalProgramsPythonPython36-32libimportlib\__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:djangoravbudserverravbudserverurls.py", line 41, in <module>
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
  File "c:djangodjangodjangourlsconf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:UsersuserAppDataLocalProgramsPythonPython36-32libimportlib\__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:UsersuserAppDataLocalProgramsPythonPython36-32libsite-packages
est_frameworkurls.py", line 24, in <module>
    url(r'^login/$', views.login, template_name, name='login'),
AttributeError: module 'django.contrib.auth.views' has no attribute 'login'

This is my 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'))
]

does anyone have an idea about my situation? thanks in advance...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You currently define the login URL as:

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

In Django 2.1, this deprecated functionality was removed.

To fix the issue, you can change the line to:

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

See the authentication views documentation for more details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...