Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Handling forms from class based view

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading