homepage login form Django

后端 未结 5 1498
攒了一身酷
攒了一身酷 2020-12-13 00:42

I want to create a homepage with a header that asks to login with username/password and a login button to login. Currently, how I have my page set up is that pressing login

5条回答
  •  遥遥无期
    2020-12-13 01:37

    You can use Django's built in log in form. It is quit easy and efficient.And it will give you some features like form validation check.

    in urls.py:

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

    in views.py:

    from django.contrib.auth import login
    from django.contrib.auth.forms import AuthenticationForm
    
    
    def loginView(request):
        if request.method == 'POST':
            form = AuthenticationForm(data=request.POST)
            if form.is_valid():
                user = form.get_user()
                login(request, user)
                return redirect('/website/profile/')
        else:
            form = AuthenticationForm()
        return render(request, 'website/login.html', {'form': form})
    

    in html page:

    {% csrf_token %} {{form.as_p}}

提交回复
热议问题