Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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 :: inheritance in python 
Python :: last element python 
Python :: #math function in python: 
Python :: hash in python 
Python :: django or flask 
Python :: pandas python tutorial 
Python :: python os.walk 
Python :: lists in python 
Python :: vstack numpy 
Python :: python __new__ 
Python :: how to run multiple python files one after another 
Python :: python default dictionary 
Python :: ceil in python3 
Python :: pyqt5 buttons 
Python :: Try using .loc[row_indexer,col_indexer] = value instead 
Python :: python input().strip() 
Python :: how to create an auto clicker in python 
Python :: grab the first letter of each string in an array python 
Python :: how to make spinning donut on python 
Python :: python port forwarding 
Python :: python range function examples 
Python :: list of single item repeated python 
Python :: python left string 
Python :: python curses resize window 
Python :: plague meaning 
Python :: python program to find sqaure root of the number 
Python :: planet earth minecraft 
Python :: python api with live ercot real time prices 
Python :: how to modify name of email from divi 
Python :: python force realod 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =