[Django]-Django – DetailView with FormMixin and initial

5👍

With FormMixin you can specify form’s initial using initial attribute:

class PostDetailView(FormMixin, DetailView):
    model = Post
    form_class = CommentForm
    template_name = "newspaper/post-detail.html"
    initial={'name': 'John'}

Or get_initial method:

def get_initial(self):
   return {"post": self.get_object() }

Leave a comment