Learn how to give Django Initial Value to the model form. In this tutorial, you will learn how to use the django initial value for the model forms. This initial value is automatically triggered when the user accessing the form HTML page. This value is not placeholders for the form they can be acts as a prefilled form value. These values can be modified according to the user’s wish.
Requirements :
pip install django
This command will install Django on your local machine.
Firstly Create a Django Project :
python -admin django startproject project
This command will create new project.
Navigate to Project folder :
cd project #projectname
This will change the current directory of the terminal to the project directory
Secondly Creating app with Demo app :
py manage.py startapp demo
Create a demo app for the Django project we have created.
Thirdly Create Urls.py in Demo app
from django.urls import path from . import views urlpatterns = [ path('', views.home, name="home") ]
Here, We are creating the routing paths for the home view this will render out the html page we have created for the model form. However the ” will represent the local host path it will render the html template as the homepage for the project on local host address . i.e. 127.0.0.1:8000
Model Creation in Demo app :
from django.db import models # Create your models here. class Student(models.Model): name = models.CharField(max_length=200) roll = models.IntegerField()
In this step, we are creating a model with the name of the Student with the two fields that are name and rank. However, the name is character field this will create a column with the name under the student table. And the roll is an integer field this will also create a new column under the Student table in the Database.
Main urls.py (include demo URLs) :
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('demo.urls')) ]
Configure the demo app URLs to main URLs by using the include function this will render the demo app paths as well along with the admin paths which are configured above
Creating Model Form :
from django import forms from django.db.models import fields from django.forms import ModelForm from .models import * class StudentForm(forms.ModelForm): class Meta: model = Student fields = '__all__'
Here we are creating a model form form for the Student model. Using the Django model form package from the forms built-in Django.
Creating View for form with initial Value
from django.shortcuts import render from .forms import * # Create your views here. def home(request): intaial_data = { 'name': 'Student1', 'roll': 123 } form = StudentForm(initial=intaial_data) mydict = { 'form': form } return render(request, 'index.html', context=mydict)
Hence, This is the important step all over the project in where we are creating a view for a model form with initial values. In the above view, we are creating the home view which returns the index.html page. Along with the templates, context consists of the initial value for the forms. By using the attributes in the StudentForm class instance we are successfully rendering the values into the inputs of forms.
Output :
