kenneth christian
kenneth christian

Reputation: 47

Custom Django Form Not Displaying - Using Wagtail

Wagtail Blog Site - Comment Model Form Issues

I'm creating a blog site, using Wagtail, and I've run in to a snag. I've added a Comment Model to my page, which I can add comments through the admin section and they display on the correct posts, but the comment form I've created is not displaying for some reason. Here's my relevant code. Any tips on where I went wrong would be greatly appreciated.

blog/models.py

class BlogPage(Page):
    date = models.DateField("Post date")
    intro = models.CharField(max_length=250)
    body = RichTextField(blank=True)
    #tag manager
    tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
    
    #get feature image
    def main_image(self):
        gallery_item = self.gallery_images.first()
        if gallery_item:
            return gallery_item.image
        else:
            return None
    
    search_fields = Page.search_fields + [
        index.SearchField('intro'),
        index.SearchField('body'),
    ]
    
    content_panels = Page.content_panels + [
        MultiFieldPanel([
            FieldPanel('date'),
            FieldPanel('tags'),
        ], heading="Blog information"),
        FieldPanel('intro'),
        FieldPanel('body'),
        InlinePanel('gallery_images', label="Gallery images"),
    ]
    
    def serve(self, request):
        # Get current page
        post = self
        
        # Get comment form
        form = CommentForm(request.POST or None)
        
        # Check for valid form
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect(request.path)
        return render_to_response(self,
                {
                                      'post': post,
                                      'form': form,
                                  },
                                  context_instance=RequestContext(request))

class Comment(models.Model):
    post = models.ForeignKey(BlogPage, related_name='comments')
    author = models.CharField(max_length=250)
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)
    approved_comment = models.BooleanField(default=False)
    
    def approve(self):
        self.approved_comment = True
        self.save()
    
    def __unicode__(self):
        return self.text
            
    def __str__(self):
        return self.text

class CommentForm(forms.ModelForm):

    class Meta:
        model = Comment
        fields = ('author', 'text',)

blog_page.html

{% extends "base.html" %}

{% load wagtailcore_tags wagtailimages_tags %}

{% block body_class %}template-blogpage{% endblock %}

{% block content %}
    <div class="section">
        <div class="container">
            <h1 class="title">{{ page.title }}</h1>
            <p class="meta subtitle">{{ page.date }}</p>

              {% with page.main_image as main_image %}
                {% if main_image %}{% image main_image fill-500x300 %}{% endif %}
              {% endwith %}
              <p>{{ main_image.caption }}</p>
                    
            <div class="hero-body subtitle">{{ page.intro }}</div>
            <div class="content">
        
                {{ page.body|richtext }}
                
                {% if page.tags.all.count %}
                    <div class="tags">
                        <h3>Tags</h3>
                        {% for tag in page.tags.all %}
                            <span class="tag is-primary is-medium is-link"><a style="color: white" href="{% slugurl 'tags' %}?tag={{ tag }}">{{ tag }}</a></span>
                        {% endfor %}
                    </div>
                {% endif %}
                 <p><a href="{{ page.get_parent.url }}">Return to blog archive</a></p>
                <hr>
                <br>
                
                <form action="" method="POST">
                    {% csrf_token %}
                    <table>
                        {{ form.as_table }}
                    </table>
                    <input class="control button is-primary" type='submit' name='submit' value='Add Comment'>
                </form>
                <br>
                
               
                
            <hr>
            <div class="section">
                {% if page.comments.all.count %}
                <h2 class='subtitle'>Comments</h2>

                    <div class="comments">
                    {% for comment in page.comments.all %}
                    

                            {% if comment.approved_comment %}
                            <div class="comment">
                                <h5 class="date">{{ comment.created_date }}</h5>
                                <strong><h3 class="title is-3">{{ comment.author }}</h3></strong>
                                <h4 class="subtitle is-5">{{ comment.text|linebreaks }}</h4>
                                <br>
                                <hr>
                            </div>
                            {% endif %}
                    
                        {% empty %}
                            <br>
                            <p>No comments yet...</p>
                    {% endfor %}
                    </div>
                {% endif %}
            </div>
        </div>
        </div>
    </div>
{% endblock %}

Now I'm getting an error saying:

  File "/home/kenneth/development/web/sites/mysite/dynamicsalesops/blog/models.py", line 88, in serve
context_instance=RequestContext(request))
TypeError: render_to_response() got an unexpected keyword argument 'context_instance'

Upvotes: 1

Views: 680

Answers (1)

gasman
gasman

Reputation: 25292

Your view_post function is never used. In Wagtail, rendering pages as HTML is handled by a serve method on the page model itself, not by a separate view function: http://docs.wagtail.io/en/v1.9/reference/pages/theory.html#anatomy-of-a-wagtail-request

Upvotes: 1

Related Questions