2

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.

11
  • 1
    It's model = Blog not models = Blog Commented Sep 2, 2019 at 9:41
  • 1
    @dirkgroten: I'm a bit surprised that Django does not derive the model from the queryset. Since one can access that with some_queryset.model. Commented Sep 2, 2019 at 9:44
  • @WillemVanOnsem true. just realised that as well. It should, since model is None be able to get it from the queryset. So the problem is elsewhere. Commented Sep 2, 2019 at 9:45
  • Can you check in your browser the source code of your page? Do you actually see the <div id="main">? Commented Sep 2, 2019 at 9:47
  • Thanks everyone for your timely reply. I did change models to model as advised by dirkgroten; but the issue is still same. And also i can see <div id="main"> in my browser source code Commented Sep 2, 2019 at 10:16

1 Answer 1

1

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 %}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Vikrant_Chauhan for your message. I added it as said but still same request.Thanks.
It shows no error. The django form won't just render in the template. But i can seen the form in the browser source code. Here is a snapshot of it : filesharing24.com/d/DmQ
File at the link is not opening, did you make a modelform for the model? where is forms.py?
An immerse thank you @Vikrant Chauhan. The issue is now resolved. A CSS class was the culprit. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.