Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:\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'

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...

like image 368
gadss Avatar asked Oct 27 '17 12:10

gadss


2 Answers

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.

like image 183
Juuso Ohtonen Avatar answered Nov 03 '22 01:11

Juuso Ohtonen


You are using the unreleased master branch of Django, which has removed the old function-based login view. You should use the released version 1.11, where they are still present (but deprecated).

The latest master of DRF has removed this reference, but you should stick with released versions anyway.

like image 39
Daniel Roseman Avatar answered Nov 03 '22 00:11

Daniel Roseman