Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django login

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')
Comment

Python Django Login register

// 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'),
]
//  
Comment

PREVIOUS NEXT
Code Example
Python :: how to sum all the values in a list in python 
Python :: truthy falsy python 
Python :: python empty list 
Python :: check if value in dictionary keys python dataframe 
Python :: sort a list python 
Python :: check if string is python code 
Python :: python program to calculate the average of numbers in a given list 
Python :: unique python 
Python :: hexdigest python 
Python :: virtual environment python 
Python :: smtp python 
Python :: speech enhancement techniques 
Python :: global var in python 
Python :: strip plot 
Python :: django create multiple objects 
Python :: matplotlib window size 
Python :: gui button in tkinter color 
Python :: Excel file format cannot be determined, you must specify an engine manually 
Python :: Python NumPy delete Function Example Deletion from 1D array 
Python :: lambda functions 
Python :: pathy python 
Python :: Python list loop tutorial 
Python :: cross validation sklearn 
Python :: TRY 
Python :: what is print in python 
Python :: local variable referenced before assignment 
Python :: Code example of Python Modulo Operator 
Python :: Ignoring invalid distribution -ip (c:python310libsite-packages) 
Python :: dictionary get all values 
Python :: add new column of dataframe 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =