Django comments framework issues - django

Im trying to implement commenting on my site using the comments framework. I've followed the documentation:
https://docs.djangoproject.com/en/dev/ref/contrib/comments/
https://docs.djangoproject.com/en/dev/ref/contrib/comments/example/
My template looks like:
{% extends "pbase.html" %}
{% load comments %}
{% block bcontent %}
<div class="main">
<< back
<!-- Image -->
<ul>
{% if image.title %}
<div class="title">{{ image.title }}</div>
{% endif %}
<ul>
<img border="0" alt="" src="{{ media_url }}{{ image.image.name }}" width="900" />
</ul>
</ul>
{% load comments %}
{% get_comment_count for photo.image object_pk as comment_count %}
<p>{{ comment_count }} comments have been posted.</p>
{% render_comment_list for photo.image object_pk %}
{% render_comment_form for photo.image object_pk %}
</div>
{% endblock %}
On my page the number of comments is show but not the comments themselves or the form. What am I missing?
Thanks

photo.image should be just image. I know image is correct because you use it elsewhere in the template, and if it were wrong, you'd notice.

Related

How to fetch and display data from Django Model without invoking the URL

I have a Class-Based ListView as shown below:
class JobByStateView(ListView):
model = State
template_name = 'jobs/jobs_by_state.html'
context_object_name = 'state_list'
ordering = ['name']
paginate_by = 15
I have added the path to urls.py file as shown below:
path('jobs/', JobByStateView.as_view(), name='job-by-state'),
And this how my template looks:
<div class="row">
<div class="col-md-4">
<ul class="list-unstyled mb-0">
{% for state in state_list %}
{% if forloop.counter|divisibleby:"5" == False %}
<li>
{{state.name}}
</li>
{% else %}
<li>
{{state.name}}
</li>
</ul>
</div>
<div class="col-md-4">
<ul class="list-unstyled mb-0">
{% endif %}
{% endfor %}
</ul>
</div>
</div>
When I try to access this templates via the url (http://localhost:8000/jobs) it works as expected and displays the data on the screen. But when I try embed this template within another template as shown below, nothing gets displayed on the web page.
{% include 'jobs/jobs_by_state.html' %}
I would like to display this template as a widget within another template.
Really appreciate, if anyone could please help me in fixing this issue.
Thank you so much in advance for your time and help!
=========================================================================
The other page template is:
{% extends 'base.html' %}
{% block page_content %}
{% include 'carousel.html' %}
{% for job in job_list %}
<div class="listing-wrapper">
<div class="listing-container border-top border-bottom">
<a href="{{ job.get_absolute_url }}">
<h2 class="heading mt-3 mb-1 mx-2 d-inline-block">{{ job.title}}</h2>
</a>
</div>
</div>
{% endfor %}
{% if is_paginated %}
<ul class="pagination justify-content-center my-4">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link bg-dark text-white" href="?page{{page_obj.previous_page_number}}">← Previous Page</a>
</li>
{% endif %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link bg-dark text-white" href="?page{{page_obj.next_page_number}}">Next Page →</a>
</li>
{% endif %}
</ul>
{% endif %}
{% include 'jobs/jobs_by_state.html' with state_list=state_list %}
{% endblock page_content %}
{% block page_sidebar %}
{% include 'simple_search_widget.html' %}
<!-- Social Sharing Buttons -->
<div class="sharethis-inline-share-buttons mt-4"></div>
<!-- Newsletter Widget -->
{% include 'newsletter_widget.html' %}
{% endblock page_sidebar %}
The view for the parent template is as shown below:
class JobList(ListView):
model = Job
template_name = "jobs/job_listings.html"
context_object_name = "job_list"
ordering = ['-published_date']
paginate_by = 10
The following solution from #Charnel and #ChidG worked.
#Shahzan, ok, well you are referring to that variable in your template like this: {% include 'jobs/jobs_by_state.html' with state_list=state_list %}. So if the variable isn't in the context, it can't be passed to the included template, and so the included template won't display anything.

how to add an image to webapp using Python flask

I need to develop a web app using python flask and I need to add an image to the view page. But I couldn't get it.please help me out of it.
In the example below the image is stored in an SQLite database. From there you can do something like:
#app.route('/show')
def show_pic():
filename = request.args.get('filename','')
t = (filename,)
cur = g.db.execute('select label from pics where filename=?', t)
label = cur.fetchone()[0]
return render_template('upload.html', filename=filename, label=label)
And then in your template:
{% macro pic_path(pic) %}
{{ url_for('return_pic', filename='%s' % pic) }}
{% endmacro %}
{% block content %}
{% if filename is defined %}
<div id="image">
<a href="{{ pic_path(filename) }}" target="_blank">
<img class="new" src="{{ pic_path(filename) }}">
</a>
</div>
{% else %}
<ul>
{% for pic in pics %}
<li class="thumb">
<a href="{{ url_for('show_pic', filename=pic.filename) }}">
<img class="thumb" src="{{ pic_path('thumb_'+pic.filename) }}">
</a>
<strong>{{ pic.label }}</strong>
</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}

Django template include a template with a variable coming from view

I have problem with Django templates. I want to create a base html, which I will use to display posts. I call a template from view, which includes an html file, where the file extends the base html.
view
def main(request):
all_posts = News.objects.all()
return render_to_response("index.html", {'all_posts': all_posts})
template -- index.html
<div id="content">
{% include 'content.html' with posts=all_posts%}
</div>
content.html
{% extends "content_base.html" %}
{% for post in posts %}
{% block date_of_post %} {{ post.date }} {% endblock %}
{% block post_author %} {{ post.author }} {% endblock %}
{% block post %} {{ post.content }} {% endblock %}
{% endfor %}
content_base.html
<div class="post">
<h2 class="title">{% block blabla %}{% endblock %}</h2>
<p class="meta"><span class="date">{% block date_of_post %}{% endblock %}</span><span class="posted">Posted by {% block post_author %}{% endblock %}</span></p>
<div style="clear: both;"> </div>
<div class="entry">
<p>
{% block post %} {% endblock %}
</p>
<p class="links">
Read More
Comments
</p>
</div>
</div>
But it seems like I cannot pass the *all_posts* variable to content.html. What is the problem here? Am I doing something wrong?
Thanks in advance.
You should move the loop into index.html, and include content_base.html directly. So index.html becomes:
<div id="content">
{% for post in posts %}
{% include 'content_base.html' %}
{% endfor %}
</div>
and content_base.html is
<div class="post">
<h2 class="title">{{ post.title }}</h2>
<p class="meta"><span class="date">{{ post.date }}</span><span class="posted">Posted by {{{ post.author }}</span></p>
<div style="clear: both;"> </div>
<div class="entry">
<p>
{{ post.content }}
</p>
<p class="links">
Read More
Comments
</p>
</div>
</div>
You're using blocks with identical names, because you're using them in a loop:
{% for post in posts %}
{% block date_of_post %} {{ post.date }} {% endblock %}
{% block post_author %} {{ post.author }} {% endblock %}
{% block post %} {{ post.content }} {% endblock %}
{% endfor %}
You can't do that. Look here.
Sometimes too much normalization is not so good. You can simply have index.html and all of your code can go there (avoiding unnecessary blocks).
<div id="content">
{% for post in all_posts %}
<div class="post">
<h2 class="title">{{post.title}}</h2>
<p class="meta"><span class="date">{{post.date}}</span><span class="posted">Posted by {{post.post_author}}</span></p>
<div style="clear: both;"> </div>
<div class="entry">
<p>
{{post.content}}
</p>
<p class="links">
Read More
Comments
</p>
</div>
</div>
{% endfor %}
</div>

django - Invalid block tag: 'add_pinned_status', expected 'else' or 'endif'

I get the following error when serving my django application using Nginx+FastCGI
Invalid block tag: 'add_pinned_status', expected 'else' or 'endif'
Oddly, the site works just fine when I'm serving using the Django development server. It also works with Nginx most of the time, but the error randomly appears and reappears with refreshes. Any idea what the problem could be?
EDIT: Here's the code, just to clarify that there's NO hanging if statement.
{% extends 'master.html'%}
{% load thumbnail %}
{% load tags %}
{% block 'title' %}
{{ title }}
{% endblock %}
{% block 'content' %}
<div id="feed" class="content">
{% for book in books.object_list %}
<div class="book_preview">
<div class="thumbnail">
<a href="/book/{{ book.id }}/{{ book.get_slug }}/">
{% if book.cover_image %}
{% thumbnail book.cover_image "120" as im %}
<img src="{{ im.url }}" alt="Python for Software Design"/>
{% endthumbnail %}
{% else %}
<img src="{{ STATIC_URL }}default_thumb.jpg" alt="Python for Software Design"/>
{% endif %}
</a>
</div>
<div class="book_details">
<h2 class="book_title">
<a class="book_profile_link" href="/book/{{ book.id }}/{{ book.get_slug }}/">{{ book.title }}</a>
{% if user != book.uploader %}
<a class="shelf_adder {% add_pinned_status request book.pk %}" href="/shelf/{{ book.id }}/toggle/?next={{ request.get_full_path }}" title="Toggle shelf status"></a>
{% endif %}
</h2>
<h3 class="book_subtitle">
{% if book.subtitle %}
{{ book.subtitle }}
{% else %}
<a href='/book/{{book.id}}/edit/#subtitle'>Provide subtitle</a>
{% endif %}
</h3>
<h3 class="book_authors"> by {{ book.author.filter|join:", " }}</h3>
<div class="book_description">
{% if book.description %}
<p>
{{ book.description|truncatewords:25 }}
</p>
{% else %}
<p class="message">No description available. Create one.</p>
{% endif %}
</div>
<div class="book_links">
<a href="/book/{{ book.id }}/{{ book.get_slug }}/" class="book_profile_link" title="Book profile">
Book profile
</a>
<a href="http://{{ book.homepage }}" class="book_website_link" title="Book website" target="_blank">
Book website
</a>
</div>
<p>Points: {{ book.shelf_additions }}</p>
<div class="book_tags">
{% if book.topics.all %}
{% for topic in book.topics.filter %}
{{ topic }}
{% endfor %}
{% else %}
<a href="/book/{{ book.id }}/edit/#topics" title='Click to add'>no topics added☹</a>
{% endif %}
</div>
</div>
<div style="clear: both;"></div>
</div>
{% endfor %}
<div class="pagination">
{% if books.has_previous %}
previous
{% endif %}
<span class="current">
Page {{ books.number }} of {{ books.paginator.num_pages }}
</span>
{% if books.has_next %}
next
{% endif %}
</div>
</div>
{% endblock %}
The problem starts on the line after the if user != book.uploader statement, which as you can see is terminated with the appropriate endif. I suspect it may be some sort of timeout but I'm not entirely sure. Keep in mind, it works sometimes but randomly stops when using Nginx. It works flawlessly with the dev server.
Django gives that error when you have an unclosed templatetag. In this case an {% if ... %} templatetag.
As to why it only happens in certain scenarios, it might be inside a conditional tag itself, so it's not always processed, but I think Django processes the whole template despite what's going on conditionally or not. It might also be possible that there was some mistake in updating your production site and it's using a different/older version than your development site.
Regardless, the error is the error. Find the unclosed templatetag, and you'll solve it across the board.
UPDATE: The alternative is that the add_pinned_sites templatetag is undefined. Assuming it is in fact loaded in {% load tags %}, make sure that that templatetag library is available in all running environments, i.e. it literally exists on the server. If it is in fact there, make sure you completely reload your Nginx+FastCGI environment, or just reboot the server to be completely sure.
Is "tags" the actual name of the tag library that holds add_pinned_sites? Might be worth changing it to a clearer name-- just wondering if it's possible you're seeing import collisions between that and another tag library (like Django's built-in tags).

Django Paginated Comments .. is there any existing solutions?

is there any existing pagination solution for Django contrib.comments?
What I need is just a simple paginated django comments, for the Basic Blog application (from the Django Basic Apps) I used, using a simple has_previous and has_next
I have copied the django.contrib.comments and tried modify the code but with no success. The code is pretty hard to understand (django/contrib/comments/templatetags/comments.py) because it consists of Node and Parser
here is my comments.html template I used for the Blog application:
{% load comments markup %}
{% get_comment_list for object as comment_list %}
{% if comment_list %}
<div class="comments g_7 left">
<a name="comments"></a>
<div class="subtitle">Comments</div>
{% for comment in comment_list %}
{% if comment.is_public %}
<div class="comment g_6" id="c{{ comment.id }}">
<div class="comment_name g_6">
<div class="comment_count right">
<a name="c{{ comment.id }}" href="{{ comment.get_absolute_url }}" {% ifnotequal comment.person_name null %}title="Permalink to {{ comment.person_name }}'s comment"{% endifnotequal %} class="comment_count">{{ forloop.counter }}</a></div>
Wrote by <strong>{% if comment.user_url %}{{ comment.user_name }}{% else %}{{ comment.user_name }}{% endif %}</strong> on {{ comment.submit_date|date:"F j, Y" }} - {{ comment.submit_date|date:"P" }}
</div>
<div class="comment_body g_6">{{ comment.comment|urlizetrunc:"60"|safe }}</div>
</div>
{% endif %}
{% endfor %}
<div class="clear"></div>
</div>
{% else %}
No comments yet.
{% endif %}
I think the problem lies in the get_comment_list templatetags :)
Thanks in advance
I think django-pagination might be what you're looking for.
http://code.google.com/p/django-pagination/ (screencast available)
Django also has a built in Pagination system
https://docs.djangoproject.com/en/dev/topics/pagination/