Home > Mobile >  Handling forms from class based view
Handling forms from class based view

Time:12-05

Hello how can I pass form into template from class based view? In HTML everything inherits and I can render elements inside block content but I can not render form. This is my code. :

views.py:

class Signup(TemplateView):
   model =  Profile
   template_name = 'home/sign-up.html'
   form_class = UserCreationForm()
   def get_context_data(self, **kwargs):
      context = super().get_context_data(**kwargs)
      context['form'] = UserCreationForm

HTML:

{% extends "home/todo.html" %}
{% block content %}
<form method="POST">
    {{form}}
</form>
    
{% endblock content %}

CodePudding user response:

Give this a try

context['form'] = self.form_class

should work

But for User creation, you may better use CreateView instead of TemplateView

from django.views.generic import CreateView

class Signup(CreateView):
    template_name = 'home/sign-up.html'
    form_class = UserCreationForm()
  • Related