Open In App

Create View - Function based Views Django

Last Updated : 10 Oct, 2025
Suggest changes
Share
16 Likes
Like
Report

A Create View in Django is a way to let users add new information into a database. When using function-based Create views:

  • A form is displayed to the user.
  • The submitted form is checked for correctness.
  • If the form is valid, the new information is saved to the database.

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 geeks folder and add the following cdode to create ModelForm for this model:

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