createview django

# Import necessary modules
from django.views.generic.edit import CreateView
from .models import YourModel
from .forms import YourModelForm

# Create a class-based view using Django's CreateView
class YourModelCreateView(CreateView):
    # Specify the model for which the view is being created
    model = YourModel

    # Specify the form class to be used for data input
    form_class = YourModelForm

    # Specify the template to be used for rendering the form
    template_name = 'your_app/your_model_form.html'

    # Specify the URL to redirect to upon successful form submission
    success_url = '/success/'  # Update with your desired success URL

    # Override the form_valid method to add custom logic upon successful form submission
    def form_valid(self, form):
        # Perform any additional actions before saving the form data
        # For example, you can add the current user as the owner of the object
        form.instance.user = self.request.user
        # Call the parent class's form_valid method to save the form data
        return super().form_valid(form)