How to Set Initial Values in Django Model Form?

Published On: 16/01/2023 | Category: Django

Hi Dev,

Are you looking for example of how to set initial values in django model form. We will use django form initial values from model. you will learn django form set initial value. you can see django set value to model field.

Here, this initial value is automatically triggered when the user accessing the form HTML page.

Here i will give you we will help you to give example of django form initial values from model. So let's see the bellow example:

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject mypro
Step 2: Create an App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

cd example
django-admin startapp core
Step 3: Update

Then update INSTALLED_APPS within our file to notify Django about the app.

    'core', #new

Step 4: Create a Model

In this step we will require the database model for storing contacts.Open the core/ file and add the following code:

from django.db import models

# Create your models here.
class Article(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()

Ok, all set. We can engender a migrations file for this change, then integrate it to our database via migrate.

python makemigrations
python migrate
Step 5: Create a Form

In this step We need to create a form that will be used .like add a bootstrap class and validation etc.. plus we need to add custom styling.

from django import forms
from django.db.models import fields
from django.forms import ModelForm

from .models import *

class ArticleForm(forms.ModelForm):
    class Meta:
        model = Article
        fields = '__all__'
Step 6: Creating the Views

In this step, we need to configure views. open the core/ file and add.

from rest_framework.views import APIView  
from django.http import JsonResponse  
from .forms import ArticleForm
from django.shortcuts import render

def home(request):
    intaial_data = {
        'title': 'Django',
        'body': 'Django Example'
    form = ArticleForm(initial=intaial_data)
    mydict = {
        'form': form
    return render(request, 'index.html', context=mydict)
Step 7: Creating the Templates

Next, then with your text editor create new templates files: core/templates/index.html file and the add:

<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
        {{ form.as_p }}
Step 8: Creating URLs

In this section, we need a file within the core app however Django doesn't create one for us with the startapp command. Create core/ with your text editor and paste below code.

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),

Next, we require to add a URL path for our core app which can be done by importing include and setting a path for it.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('core.urls')),
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/ address with a web browser.

I Hope It will help you....