Django forms are like an advanced
Rendering Django forms in the template might be messy at times. However, with a good knowledge of Django forms and attributes of fields, you can easily create an excellent form with powerful features.
The Django widget tweaks is used to render form fields in templates. This allows you to adjust the properties of the form (such as method and CSS class) on the back-end without rewriting the template.
Let’s start with the installation.
pip install pipenv
pipenv shell
pipenv install django
Use ./
at the end to create the project in the same directory. Then, use startapp codebase
to create an app for the project.
django-admin startproject DjangoWidgetTweak ./
python manage.py startapp codebase
python manage.py migrate
python manage.py runserver
Go to settings.py and paste the following:
INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',# adding the codebase app'codebase'# adding the widget tweak in the installed app'widget_tweaks']
In the codebase app folder, go to models.py to create a model for the database.
from django.db import modelsclass WidgetModel(models.Model):name = models.CharField(max_length=400)age = models.IntegerField()def __str__(self):return self.name
Go to the admin.py file to register the model in the admin view.
from django.contrib import adminfrom .models import WidgetModeladmin.site.register(WidgetModel)
Create a file and name it forms.py in order to make use of Django forms.
from django import formsfrom django.db.models import fieldsfrom .models import WidgetModelclass WidgetForm(forms.ModelForm):class Meta:model = WidgetModelfields = '__all__'
The Views.py file:
from django.shortcuts import redirect, renderfrom .forms import WidgetFormfrom django.http import HttpResponsedef home(request):form = WidgetForm(request.POST or None)if request.method == 'POST':if form.is_valid():form.save()return redirect('home')else:return HttpResponse('form not valid')context = {'form': form}return render(request, 'app/home.html', context)
In the codebase app, create a folder and name it templates
.
Inside the templates
folder, create another folder and name it app
.
Inside the app
folder, create the home.html
file.
In the DjangoWidgetTweak
folder, in the urls.py
file, do the following:
from django.contrib import adminfrom django.urls import path, includeurlpatterns = [path('admin/', admin.site.urls),# including the codebase folder 'urls.py' filepath('', include('codebase.urls'))]
Then in the codebase
folder, create a file and name it urls.py</a>
:
from django.urls import path#importing home function from views.pyfrom .views import homeurlpatterns = [path('', home, name='home'),]
Run the following commands:
makemigrations
to prepare the model for migrations.
migrate
to move the model and any other related packages that need to be moved to the database.
python manage.py makemigrations
python manage.py migrate
In case you want to create a super-user, run the following command.
python manage.py createsuperuser
After it, to start the server, run this:
python manage.py runserver
Then, go to http://127.0.0.1:8000/admin
and login in order to access the admin panel.