CreateView with ModelForm

Use CreateView to build create forms with success URLs.


# models.py

from django.db import models

class Ticket(models.Model):
    title = models.CharField(max_length=120)
    description = models.TextField(blank=True)
  

# admin.py

from django.contrib import admin
from .models import Ticket

@admin.register(Ticket)
class TicketAdmin(admin.ModelAdmin):
    list_display = ("title",)
  

# views.py
from django.views.generic import CreateView
from django.urls import reverse_lazy
from .models import Ticket
class TicketCreate(CreateView):
    model = Ticket
    fields = ["title","description"]
    template_name = "tickets/form.html"
    success_url = reverse_lazy("ticket-list")

# urls.py
path("tickets/new/", TicketCreate.as_view(), name="ticket-create")
  
Explanation:
  • Set success_url with reverse_lazy to avoid import cycles.
  • Use fields or form_class (ModelForm).
  • Category Views (FBV & CBV)
  • Total Views 405
  • Last Modified 22 December, 2025
  • Tags #cbv #createview #forms #models
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.