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.

Suggested Quiz
2 Questions

In a function-based Create View, what is the required sequence when handling a POST request to create a new record?

  • A

    Validate submitted form data → save new object → redirect or render response

  • B

    Save data → validate form → render template

  • C

    Show empty form → redirect to login → save data

  • D

    Delete old records → save new object → return blank form

Explanation:

On POST, submitted data must be validated; if valid, new object is saved, then response (often redirect) is returned.

Which file usually contains the view function definition for a Create View in a Django app?

  • A

    urls.py

  • B

    views.py

  • C

    settings.py

  • D

    models.py

Explanation:

Django app’s views (including Create Views) are normally defined in the views.py file.

Quiz Completed Successfully
Your Score :   2/2
Accuracy :  0%
Login to View Explanation
1/2 1/2 < Previous Next >

Explore