1

I have a custom article.html template that is overriding the default article.html template that comes with the aldryn newsblog plugin. In this template I have 2 lines to include some custom functionality:

{% include 'comment_form.html' %}
{% include 'list_comments.html' %}

Here are those 2 template files in detail:

list_comments.html template:

{% load cms_tags staticfiles sekizai_tags %}
{% if comments %}
    {% for item in comments %}
    <div class="comment paragraph">
        <h4>{{ item.author }}</h4>
        <p>{{ item.comment }}</p>
        <p>{{ item.date }}</p>
    </div>
    {% endfor %}
{% else %}
    <p>No comments exist on this blog. Be the first to comment!</p>
{% endif %}
{{ another }}
{{ thisdoesntexist }}

and comment_form.html

{% load cms_tags staticfiles sekizai_tags %}
<div id="comment_form">
<div class="container constrained paragraph">
    <h5>Submit a comment</h5>
    <form method="post">
        {% csrf_token %}

        {{ comment_form }}

        <input type="hidden" name="page" value="{{ article.id }}">

        <input type="submit" value="Submit Comment">
    </form>
</div>

And views.py I have 2 functions to return views with context variables:

def display_form(request):
    comment_form = CommentForm()

    return render(request, 'comment_form.html', {'comment_form': comment_form})


def get_blog_comments(request):
    qs = BlogComment.objects.all()
    context = {'comments': qs, 'another': 'TEST STRING'}
    return render(request, 'list_comments.html', context)

And in both templates, the context variables are outputting nothing. I am at a loss for what I'm doing wrong. I copied the code almost directly from the django tutorial on forms. django.template.context_processors.request is included in my settings.py context_processors.

I'm not sure if my {{ thisdoesntexist }} variable would throw an error or not, but it's not. the templates are displaying all other html except the context variables, so it's not a template linking issue.

EDIT:

urls.py

urlpatterns = [
url(r'^filer/', include('filer.urls')),
url(r'^location/', include('locations.urls')),
url(r'^captcha/', include('captcha.urls')),
url(r'^', include('cms.urls')),
]
pinksharpii
  • 527
  • 1
  • 8
  • 18
  • You still have a fundamental misunderstanding about how things work. Did you do the Django tutorial? As I've told you before, you can't expect just to mention variables in a template and expect them to somehow be populated. Templates don't call views, URLs call views which then render templates. The only thing that will call the `get_blog_comments` view would be a URL mapped to that view which would then render the list_comments template. Including that template in another one wouldn't do anything to call the view, because that just isn't how it works. – Daniel Roseman Aug 21 '18 at 16:10
  • @DanielRoseman I'm not asking the template to call the view, from my understanding of the documentation, passing in that template variable in the view's render function, should make the template variables accessible in my templates. This is how I've seen all examples setup. But saying that a URL needs to be mapped to a view doesn't make any sense. I should be able to render comments for an article within that article's page, not its own URL just for comments. – pinksharpii Aug 21 '18 at 16:35
  • Buy that's not what I said at all. Of course you can render ah article and its comments on the she paste *as long as you pass the relevant information in the context from that same view*. You just can't have two views somehow being called for the same page. And I'm deeply worried that you think this doesn't make sense; as I say, the fact that a single URL maps to a single view is fundamental to how Django works. – Daniel Roseman Aug 21 '18 at 22:33
  • @DanielRoseman If I hadn't read through the documentation and many tutorial sites across the internet, do you think I would be here? Obviously something is not setup right, but you telling me I'm not doing it right isn't helping anybody. I've read through my resources and clearly there's something I'm not getting and I need to communicate my questions with an actual human being, since documentation can only get me so far. Now, are you saying the view that returns the context for the article's page has to be the one that returns the form and comments, that I can't do it in my own view? – pinksharpii Aug 21 '18 at 23:49
  • @DanielRoseman I wrote my question all sorts of backwards, I apologize. Are you saying that the view for the article itself has to be the one that returns the form and the comments and that I can't do it in my custom code because the article view is written in a separate plugin? – pinksharpii Aug 22 '18 at 00:03

1 Answers1

0

The context variables aren't showing because you didn't pass in either include statement.

{% include 'comment_form.html' with comment_form=comment_form %}
{% include 'list_comments.html' with comments=comments %}
Mint
  • 1,013
  • 1
  • 12
  • 29