Utilisateur de déconnexion de Django
def logout_request(request):
logout(request)
return redirect("")
Virgin Programmer
def logout_request(request):
logout(request)
return redirect("")
def login_view(request):
if request.method == 'GET':
cache.set('next', request.GET.get('next', None))
if request.method == 'POST':
# do your checks here
login(request, user)
next_url = cache.get('next')
if next_url:
cache.delete('next')
return HttpResponseRedirect(next_url)
return render(request, 'account/login.html')
#loginView
from django.contrib.auth.views import LoginView
class AdminLogin(LoginView):
template_name = 'LoginView_form.html'
from django.contrib.auth.mixins import LoginRequiredMixin
class MyView(LoginRequiredMixin, View):
login_url = '/login/'
redirect_field_name = 'redirect_to'
// go to templates - includes folder - alerts.html
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{message.tags}}"{% endif %}>
{% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %} Important: {% endif %}
{{ message }}
</li>
{% endfor %}
</ul>
{% endif %}
// go to settings.py
from django.contrib.messages import constants as messages
MESSAGE_TAGS = {
messages.INFO: 'danger',
}
// go to templates - accounts folder - register.html
<center>
<div class="col-lg-5 order-1 order-lg-2">
<div class="row justify-content-md-center">
<div class="col-md-9" style="margin-bottom: 250px; margin-top: 150px;">
<div class="featured-box featured-box-primary text-start mt-0">
<div class="box-content">
{% include 'includes/alerts.html'%}
<h4 class="color-primary font-weight-semibold text-4 text-uppercase mb-3">Register An Account</h4>
<form action="{% url 'register'%}" id="frmSignUp" method="POST" class="needs-validation">
{% csrf_token %}
<div class="row">
<div class="form-group col-lg-6">
<label class="form-label">First Name</label>
{{ form.first_name }}
</div>
<div class="form-group col-lg-6">
<label class="form-label">Last Name</label>
{{ form.last_name }}
</div>
</div>
<div class="row">
<div class="form-group col-lg-6">
<label class="form-label">Email</label>
{{ form.email }}
</div>
<div class="form-group col-lg-6">
<label class="form-label">Phone Number</label>
{{ form.phone_number }}
</div>
</div>
<div class="row">
<div class="form-group col-lg-6">
<label class="form-label">Password</label>
{{ form.password}}
</div>
<div class="form-group col-lg-6">
<label class="form-label">Re-enter Password</label>
{{ form.confimr_password}}
</div>
</div>
<div class="row">
<div class="form-group col-lg-9">
<div class="custom-control custom-checkbox">
<input type="checkbox" name="terms" class="custom-control-input" id="terms">
<label class="custom-control-label text-2" for="terms">I have read and agree to the <a href="#">terms of service</a></label>
</div>
</div>
<div class="form-group col-lg-3">
<input type="submit" value="Register" class="btn btn-primary btn-modern float-end" data-loading-text="Loading...">
</div>
</div>
{{ form.email.errors}}
{{ form.non_field_errors }}
</form>
</div>
</div>
</div>
</div>
</div></center>
// go to accounts folder APP - forms.py
from django import forms
from .models import Account
class RegistrationForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput(attrs={
'placeholder': 'Enter Password',
'class': 'form-control'
}))
confimr_password = forms.CharField(widget=forms.PasswordInput(attrs={
'placeholder': 'Confirm Password'
}))
class Meta:
model = Account
fields = ['first_name', 'last_name', 'phone_number', 'email', 'password']
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
self.fields['first_name'].widget.attrs['placeholder'] = 'Enter Firstname'
self.fields['last_name'].widget.attrs['placeholder'] = 'Enter Lastname'
self.fields['email'].widget.attrs['placeholder'] = 'Enter Email'
self.fields['phone_number'].widget.attrs['placeholder'] = 'Enter Phone Number'
for field in self.fields:
self.fields[field].widget.attrs['class'] = 'form-control'
// Use to verify match password
def clean(self):
cleaned_data = super(RegistrationForm, self).clean()
password = cleaned_data.get('password')
confimr_password = cleaned_data.get('confimr_password')
if password != confimr_password:
raise forms.ValidationError(
"Password does not match!"
)
// go to accounts folder APP - urls.py
from django.urls import path
from . import views
urlpatterns = [
path('register/', views.register, name='register'),
path('login/', views.login, name='login'),
path('logout/', views.logout, name='logout'),
]
//
class Login(LoginView):
template_name = "registration/login.html"
def get_context_data(self, **kwargs):
context = super(Login,self).get_context_data(**kwargs)
page_title = 'Login'
context.update({
"page_title":page_title
})
return context