Open In App

Create View - Function based Views Django

Last Updated : 18 Nov, 2025
Comments
Improve
Suggest changes
16 Likes
Like
Report

A Create View in Django allows adding new records to the database through a form.

  • Shows a form for entering new data.
  • Checks the submitted data for correctness.
  • Saves the new record to the database if the form is valid.
  • Provides a simple way to collect and store user input.

Example: Consider a project named 'geeksforgeeks' having an app named 'geeks'. After you have a project and an app, let's create a model of which we will be creating instances through our view.

In geeks/models.py:

Python
from django.db import models
 
class GeeksModel(models.Model):

    # fields of the model
    title = models.CharField(max_length = 200)
    description = models.TextField()

    def __str__(self):
        return self.title

After creating this model, we need to run two commands in order to create Database.

Python manage.py makemigrations
Python manage.py migrate

Create a forms.py file in the geeks folder and add the following code to create a ModelForm for this model, as ModelForm automatically generates form fields based on the model’s fields and is used to collect and validate user input.

Python
from django import forms
from .models import GeeksModel

class GeeksForm(forms.ModelForm):

    class Meta:
        model = GeeksModel
        fields = [
            "title",
            "description",
        ]

With the backend setup complete, create a view and template to handle the form in geeks/views.py:

Python
from django.shortcuts import render
from .models import GeeksModel
from .forms import GeeksForm

def create_view(request):
    context = {}

    form = GeeksForm(request.POST or None)
    if form.is_valid():
        form.save()
        
    context['form'] = form
    return render(request, "create_view.html", context)

Create a template in templates/create_view.html:

html
<form method="POST" enctype="multipart/form-data">

    <!-- Security token -->
    {% csrf_token %}

    <!-- Using the formset -->
    {{ form.as_p }}
    
    <input type="submit" value="Submit">
</form>

Visit development server: http://localhost:8000/django-create-view-function-based

Let's try to enter data in this form:

create-view-function-enter-data

Create view is working and we can verify it using the instance created through the admin panel.

django-mopdel-created-create-view

This approach demonstrates how to create a Create View for a model in Django.


Explore