django Login Form code example
Example 1: django login
from django.contrib.auth import authenticate, login
def my_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
...
else:
...
Example 2: from django.contrib.auth.decorators import authenticate, login
from django.contrib.auth import authenticate, login
Example 3: loginrequiredmixin
from django.contrib.auth.mixins import LoginRequiredMixin
LOGIN_URL = 'your_url'
Example 4: how to make a nice login django form
{% extends 'main/header.html' %}
{% block content %}
<div class="container">
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<button style="background-color:#F4EB16; color:blue" class="btn btn-outline-info" type="submit">Login</button>
</form>
Don't have an account? <a href="/register" target="blank"><strong>register here</strong></a>!
</div>
{% endblock %}
Example 5: import login required
from django.contrib.auth.decorators import login_required
Example 6: loginrequiredmixin django
from django.contrib.auth.mixins import LoginRequiredMixin
class MyView(LoginRequiredMixin, View):
login_url = '/login/'
redirect_field_name = 'redirect_to'