Render UserCreationForm to Bootstrap on Django

3 min read

membuat tampilan standar form django yang terlihat kaku dengan mengganti menggunakan form bootstrap akan nampak lebih indah, terlebih jika sudah menggunakan fitur inherite class yang sudah disediakan oleh django, namanya UserCreationForm

namun setelah saya coba render menggunakan bootstrap ada beberapa fields yang tidak bisa terbaca, seperti kasus ini saya ingin membungkus fields password1 dan password2 agar mengikuti alur dari bootstrap namun tidak bisa terender

script asli dari register page

original-register.html

                             
By Creating an Account you Agree to our Terms and Conditions and our Privacy Policy.
Don't have an account? Sign In Here

forms.py

from django.contrib.auth.models import User
from django.forms import ModelForm
from django.contrib.auth.forms import UserCreationForm
from django import forms
from .models import *


class OrderForm(ModelForm):
    class Meta:
        model = Order
        fields = '__all__'


class CreateUserForm(UserCreationForm):


    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']
        widgets = {
            'username': forms.fields.TextInput(
                attrs={
                    'id': 'us1',
                    'placeholder': 'Username',
                    'class': 'form-control'
                }
            ),

            'email': forms.fields.TextInput(
                attrs={
                    'id': 'email',
                    'placeholder': 'Email',
                    'class': 'form-control'
                }
            ),

            'password1': forms.PasswordInput(
                attrs={
                    'id': 'pass',
                    'placeholder': 'Password',
                    'class': 'form-control'
                }
            ),
            'password2': forms.PasswordInput(
                attrs={
                    'id': 'cn-pass',
                    'placeholder': 'Confirm Password',
                    'class': 'form-control'
                }
            ),
        }

kemudian saya terapkan pada template django

register.html

                                
{% csrf_token %}
{{form.username}}
{{form.email}}
{{form.password1}}
{{form.password2}}
By Creating an Account you Agree to our Terms and Conditions and our Privacy Policy.
Don't have an account? Sign In Here

views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.forms import inlineformset_factory
from .models import *
from .forms import OrderForm, CreateUserForm
from .filters import OrderFilter
from django.contrib.auth.forms import UserCreationForm

# Create your views here.


def registerPage(request):
    form = CreateUserForm()

    if request.method == 'POST':
        form = CreateUserForm(request.POST)
        if form.is_valid():
            form.save()

    context = {
        'form': form,

    }
    return render(request, 'accounts/register.html', context)

namun hasilnya tidak sesuai dengan yang diharapkan dimana fields password1 dan password2 masih menggunakan bawakan UserCreationForm Django

maka dari itu ada beberapa hal yang perlu diperbaiki pada

forms.py

from django.contrib.auth.models import User
from django.forms import ModelForm
from django.contrib.auth.forms import UserCreationForm
from django import forms
from .models import *


class OrderForm(ModelForm):
    class Meta:
        model = Order
        fields = '__all__'


class CreateUserForm(UserCreationForm):
    password1 = forms.CharField(max_length=16, widget=forms.PasswordInput(
        attrs={'class': 'form-control', 'placeholder': 'Password from numbers and letters of the Latin alphabet'}))
    password2 = forms.CharField(max_length=16, widget=forms. (
        attrs={'class': 'form-control', 'placeholder': 'Password confirm'}))

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']
        widgets = {
            'username': forms.fields.TextInput(
                attrs={
                    'id': 'us1',
                    'placeholder': 'Username',
                    'class': 'form-control'
                }
            ),

            'email': forms.fields.TextInput(
                attrs={
                    'id': 'email',
                    'placeholder': 'Email',
                    'class': 'form-control'
                }
            ),

            # 'password1': forms.PasswordInput(
            #     attrs={
            #         'id': 'pass',
            #         'placeholder': 'Password',
            #         'class': 'form-control'
            #     }
            # ),
            # 'password2': forms.PasswordInput(
            #     attrs={
            #         'id': 'cn-pass',
            #         'placeholder': 'Confirm Password',
            #         'class': 'form-control'
            #     }
            # ),
        }

hasilnya

How to Run Django on Jupyter Notebook Visual Code

previously I have struggled to debug my app when running Python app directly; I need it to run on my Jupyter Workspace easily to...
admin
27 sec read

How to secure important data with environments in Python

python file .env
admin
19 sec read

Custome Setting Database Django

berikut ini macam cara settting database django MAMP Server 2. MYSQL Server (XAMPP) 3. Default
admin
13 sec read

Leave a Reply

Your email address will not be published. Required fields are marked *