Django cannot import login from django.contrib.auth.views

前端 未结 5 1682
长发绾君心
长发绾君心 2020-12-01 10:38

I try to build a login function for my page. To edit the urls.py as followed, it keeps printing this:

cannot import name \'login\' from \'django.contrib.

相关标签:
5条回答
  • 2020-12-01 11:16

    try this

    app_name = 'users'
    
    urlpatterns = [
        url(r'^login/$', LoginView.as_view(template_name='users/login.html'), name='login'),
    ]
    
    0 讨论(0)
  • 2020-12-01 11:22

    You can try with this code:

    from django.urls import path
    from django.contrib.auth import views as auth_views
    
    from . import views
    
    app_name = 'users'
    urlpatterns = [
        # Login page.
        path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
    ]
    
    0 讨论(0)
  • 2020-12-01 11:23

    Since django-1.11, the login, logout, etc. function-based views have been rewritten to class-based views: the LoginView [Django-doc] and LogoutView [Django-doc] classes, as is specified in the release notes. The "old" function-based views could still be used, but were marked as deprecated.

    In django-2.1, the old function-based views have been removed, as specified in the release notes.

    You can write it like:

    from django.contrib.auth.views import LoginView
    
    from django.urls import path
    from . import views
    app_name = "users"
    urlpatterns = [
        path('login/', 
            LoginView.as_view(
                template_name='users/login.html'
            ), 
            name="login"
        ),
    ]
    0 讨论(0)
  • 2020-12-01 11:29

    Can try this to create a login form

    # views page
    from django.contrib.auth.forms import UserCreationForm
    from django.contrib.auth import login
    from django.contrib import messages
    
    def loginPage(request):
        if request.method == "POST":
            username = request.POST.get("username")
            password = request.POST.get("password")
            user = authenticate(request, username=username, password=password)
            if user is not None:
                login(request, user)
                return redirect('home')
            else:
                messages.info(request, 'Username or Password is incorrect')
        context = {}
        return render(request, 'accounts/login.html', context)
    
    #urls
    urlpatterns = [
    path('login/', views.loginPage, name='login'),,
    ]
    
    0 讨论(0)
  • 2020-12-01 11:33

    @Willem Van Onsem's answer worked for me. On an implementation note, if you rather keep your view code separate from urls (also if you have some processing to do), you would write your urls.py like this (based on a per-app urls.py file in your app folder which means you have to include it in the overall urlpatterns of the project's urls.py file which is in the same folder as your settings.py file, with the syntax path('', include('users.urls')),):

        from django.urls import path
    
        from .views import (
            login_view
        )
    
        app_name = "userNamespace"
        urlpatterns = [
          path('login/', loginView.as_view(), name="login-view"),
        ]
    

    and over in your views.py file you would have something like this:

    from django.shortcuts import render
    from django.contrib.auth.views import (
        LoginView,
    )
    from users.models import User
    
    class login_view(LoginView):
        # The line below overrides the default template path of <appname>/<modelname>_login.html
        template_name = 'accounts/login.html' # Where accounts/login.html is the path under the templates folder as defined in your settings.py file
    
    0 讨论(0)
提交回复
热议问题