Question

I've only been at python for about 2 weeks now and I've run into an issue which I've been trying to figure out for the past 3 days. I've read through the official documentation and pretty much every tutorial and youtube video available.

I'm trying to build a simple blog as a first project and would like to have a section where people can post comments. I have set up the comments model and a modelform to go along with it. However, I can't figure out how to get django to create the form for me in the template, nothing is displayed.

models.py

    from django.db import models
    from django.forms import ModelForm

    class posts(models.Model):
        author = models.CharField(max_length = 30)
        title = models.CharField(max_length = 100)
        bodytext = models.TextField()
        timestamp = models.DateTimeField(auto_now_add=True)

        def __unicode__(self):
            return self.title

    class comment(models.Model):
        timestamp = models.DateTimeField(auto_now_add=True)
        author = models.CharField(max_length = 30)
        body = models.TextField()
        post_id = models.ForeignKey('posts')

        def __unicode__(self):
            return self.body

    class commentform(ModelForm):
        class Meta:
            model = comment

views.py

from django.shortcuts import render_to_response
from blog.models import posts, comment, commentform
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf


def home(request):
    entries = posts.objects.all()
    return render_to_response('index.html', {'posts' : entries})

def get_post(request, post_id):
    post = posts.objects.get(id = post_id)
    context = {'post': post}
    return render_to_response('post.html', context)

def add_comment(request, post_id):
    if request.method == 'POST':
        form = commentform(request.POST)
        if form.is_valid():
            new_comment = form.save(commit = false)
            new_comment.post_id = post_id
            new_comment.save()
    else:
        form = commentform()

    context = RequestContext(request, {
        'form': form})

    return render_to_response('post.html', context)

Urls.py

    from django.conf.urls import patterns, include, url
    from django.conf import settings
    from django.contrib import admin
    admin.autodiscover()

    urlpatterns = patterns('',
        url(r'^$', 'blog.views.home', name='home'),
        url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
        url(r'^admin/', include(admin.site.urls)),
        url(r'^(?P<post_id>.*)/$', 'blog.views.get_post'),
        url(r'^post_comment/(\d+)/$','blog.view.add_comment'),

post.html

{% extends 'base.html' %}

{% block content %}
    <p><h3> {{ post }} </h3></p>
    <p> {{ post.bodytext }} </p>

<br><br><br><br>

<form action="/post_comment/{{ post.id }}/" method="POST"> {% csrf_token %}
    {{ form }}
    <input type="submit" value="Post">
</form>

{% endblock %}

I do get a 403 error CSRF verification failed but, the more pressing issue I think is that the {{ form }} doesn't do anything in the template.

I believe home function and get_post function are working and all the urls are working properly. So, I assume there's something wrong with the add_comment function or in posts.html.

Thanks

Was it helpful?

Solution

Django isn't magic. Your comment form is in a completely different view from the one that displays the blog post. So how is Django supposed to know to render it? You need to actually pass the form object to the template somehow.

The way that this was done in the old contrib.comments app was to have a simple template tag which was responsible for just displaying the comment form for an object, which you could place on any template. The form itself submitted to its own view as usual.

OTHER TIPS

One thing you might check is the syntax of the render_to_response call. I believe you'll want to define it like this. (Maybe your syntax will work too, and this isn't the issue, but I have mostly seen the call made like this). This could be the cause of the missing form in the context.

return render_to_response('post.html',
                      {'form': form},
                      context_instance=RequestContext(request))

Let me know if this works. Hope this helps, Joe

Reference: https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.render_to_response

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top