Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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'),
]
//  
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Python #Django #Login #register
ADD COMMENT
Topic
Name
6+5 =