Not showing the Form in html

Viewed 33

This is my model

class showroom(models.Model):
 serialno=models.IntegerField(db_index=True,primary_key=True)
 carname=models.CharField(max_length=50)
 carmodel=models.CharField(max_length=50)
 price=models.IntegerField()
 rating=models.IntegerField(validators=[MinValueValidator(1),MaxValueValidator(5)])

This is my forms.py

class RegisterForm(ModelForm):
 class Meta:
    model = showroom
    fields =("__all__")

views.py

class carform(CreateView):
 model = showroom
 form_class=RegisterForm
 template_name = "carbrand/carregister.html"
 context_object_name='registerform'

html page

{% block content %}
    {{registerform}}
{% endblock content %}

It's just showing a blank screen. I have imported all the necessary classes and views. It will make this too long for you so i removed it.can anyone please tell me if anything wrong in my view/form.

1 Answers

The name of the form is not determined by the context_object_name. In fact in a CreateView, the context_object_name does not change anything since no object is passed.

A FormView (and its descendants), always pass the form that is constructed as form to the context. You thus should render this with:

{% block content %}
  <form method="post" action="{% url 'some-view-name' %}">
    {% csrf_token %}
    {{ form }}
    <button type="submit">submit</button>
  </form>
{% endblock content %}
Related