user12008968
user12008968

Reputation:

Django Forms Not Rendering In HTML

I am finding it difficult to show django forms in html template. The django form fails to render in the template. I am using class base view and below are my codes for views.py,urls.py, models.py and the html template:

views.py

class Home(CreateView):
     models = Blog
     queryset = Blog.objects.filter(publish = True)
     template_name='index.html'
     fields = '__all__'

urls.py

urlpatterns=[
    path('', Home.as_view(), name='index'),
]

models.py

Continent = (
     ('select continent', 'Select Continent'),
     ('africa', 'Africa'),
     ('europe', 'Europe'),
     ('north america', 'North America'),
     ('south america', 'South America'),
     ('asia', 'Asia'),
     ('australia', 'Australia'),
     ('Antarctica', 'Antarctica'),   
)

 class Blog(models.Model):
     name= models.CharField(max_length = 200)
     company= models.CharField(max_length = 200)
     post = models.CharField(max_length = 200)
     author= models.ForeignKey('auth.User', on_delete = models.PROTECT)
     mantra= models.CharField(max_length = 200, help_text='make it short 
                              and precise')
     continent = models.CharField(choices= Continent, default= 'select 
                               continent', 
                               help_text='help us to know you even more', 
                               max_length=50)
     publish = models.BooleanField(default =True)


    def __str__(self):
        return self.name

    def get_absolute_url(self): # new
       return reverse('post_detail')

index.html

{% extends "base.html" %}
{% load static %}

{% block content %}
 <body class="loading">
    <div id="wrapper">
        <div id="bg"></div>
        <div id="overlay"></div>
        <div id="main">

             {{form.as_p}}

             {% include "partials/_footer.html" %}
        </div>
      </div>
  </body>
{% endblock %}

Any assistance will be greatly appreciated. Thanks.

Upvotes: 2

Views: 75

Answers (1)

Vikrant Chauhan
Vikrant Chauhan

Reputation: 43

You need to add csrf token to your template

{% extends "base.html" %}
{% load static %}

{% block content %}
 <body class="loading">
    <div id="wrapper">
        <div id="bg"></div>
        <div id="overlay"></div>
        <div id="main">
             {% csrf_token %}
             {{form.as_p}}

             {% include "partials/_footer.html" %}
        </div>
      </div>
  </body>
{% endblock %}

Upvotes: 1

Related Questions