I am trying to implement a comment section on my app that keeps track of bookmarks.
This is the output that I got when I ran my local host server. It is not what I expected and I can't find out what's wrong. I expected there to be a comment form, a number for the comment count, and a comment list. But the webpage just rendered my code.
This is the code
{% extends "base.html" %}
{% load comments %}
{% block title %}Bookmark:
{{ shared_bookmark.bookmark.title|escape }}{% endblock %}
{% block head %}
<h2> Comments</h2>
{% get_comment_count for bookmarks.sharedbookmark
shared_bookmark.id as comment_count %}
{% get_comment_list for bookmarks.sharedbookmark
shared_bookmark.id as comment_list %}
{% for comment in comment_list %}
<div class="comment">
<p><b>{{ comment.user.username }}</b> said:</p>
{{ comment.comment|escape|urlizetrunc:40|linebreaks }}
</div>
{% endfor %}
<p>Number of comments: {{ comment_count }}</p>
{% comment_form for bookmarks.sharedbookmark
shared_bookmark.id %}
{% endblock %}
Thanks for your help!
Related
I've been stuck on this for a while, can't seem to fix the error. I've checked the code a hundred times but obviously there is something I'm missing. I have installed my app also.
Can anybody see what I'm missing?
views.py
def survey_details(request, id=None):
context = {}
surveys = Survey.objects.get(id=id)
context['survey'] = survey
return render(request, 'surveydetails.html', context)
feedback.urls.py
path('details/<int:id>', views.survey_details, name="surveydetails"),
surveys.html
{% extends 'main.html' %}
{% block content%}
<h1>Surveys</h1>
<h2>list of {{title}} </h2>
{% if surveys %}
<ul>
{% for survey in surveys %}
<li>
{{ survey.title }}
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no surveys.</p>
{% endif %}
{% endblock %}
surveydetails.html
{% extends 'main.html' %}
{% block content%}
<h1>Surveys</h1>
<h2>Detail page</h2>
<h3>{{question.title}}</h3>
<p>Details page of {{question.title}}</p>
{% endblock %}
Here you are not passing the survey id.
{% for survey in surveys %}
<li>
{{ survey.title }}
</li>
{% endfor %}
So, I have a number of objects I wish to render in a loop. I.E. Render each of the 5 latest posts on the home page. Each of these posts will be displayed differently whether or not the user is logged in.
I have a question: How would I go about making this distinction? I imagine having a template like this
{% if user.is_logged_in %}
{% for post in latest_posts %}
post.render_long_form
{% endfor %}
{% else %}
{% for post in latest_posts %}
post.render_short_form
{% endfor %}
{% endif %}
How can I make the functions render_short_form and render_long_form return the appropriate HTML snippits? I would like them to call other templates for rendering under the hood.
Thanks!
Why don't not use {% include %} tag?
{% if user.is_logged_in %}
{% for post in latest_posts %}
{% include 'long_form.html' %}
{% endfor %}
{% else %}
{% for post in latest_posts %}
{% include 'short_form.html' %}
{% endfor %}
{% endif %}
Or, more DRY version:
{% for post in latest_posts %}
{% if user.is_logged_in %}
{% include 'long_form.html' %}
{% else %}
{% include 'short_form.html' %}
{% endif %}
{% endfor %}
I have made it possible for users on my website to upload a post, and to see all the other posts from other users as well. The below code attaches the user, who wrote the post, userpicture.
I wanted it to be a link to that user. My problem is that the below code links to the current user, and not the user which created the post.
Anyone who has some ideas of how to fix this? Thank you!
{% if item.sender.userpicture_set.all %}
{% for item in item.sender.userpicture_set.all %}
{% if item.active %}
{% if forloop.first %}
{% if forloop.last %}
<a href='/members/{{ user.username }}'><img src='{{ MEDIA_URL }}{{ item.image }}' class='img-responsive' id='post-userpicture'/></a>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
<small><a href='/members/{{ user.username }}'>{{ item.sender }}</a><span style='color: grey;'> {{ item.sent }}</span></small>
{% endif %}
In your item model, add a field username, which stores the username of the user who made this post.
Here, it creates a link to the current user, because "user" doesn't have any information about the post. It has information about the current user.
After adding the 'username' field,
{% if item.sender.userpicture_set.all %}
{% for item in item.sender.userpicture_set.all %}
{% if item.active %}
{% if forloop.first %}
{% if forloop.last %}
<a href='/members/{{ item.sender.username }}'><img src='{{ MEDIA_URL }}{{ item.image }}' class='img-responsive' id='post-userpicture'/></a>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
<small><a href='/members/{{ item.sender.username }}'>{{ item.sender }}</a><span style='color: grey;'> {{ item.sent }}</span></small>
{% endif %}
UPDATE:
I have change to item.sender.username. This should work.
The problem is that you are referencing the user object in your link as <a href='/members/{{ user.username }}'>, the correct url should be something like <a href='/members/{{ item.sender.username }}'> but it requires you to have a ForeignKey reference to the user in the item model.
The django contrib comments form i'm using:
{% get_comment_form for post as form %}
<form action="{% comment_form_target %}" method="post">{% csrf_token %}
{% if next %}
<div><input type="hidden" name="next" value="{{ next }}" /></div>
{% endif %}
{% for field in form %}
{% if field.is_hidden %}
<div>{{ field }}</div>
{% else %}
{% if field.name == 'comment' %}
{% if field.errors %}{{ field.errors }}{% endif %}
<p
{% if field.errors %} class="error"{% endif %}
{% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
{{ field.label_tag }} {{ field }}
</p>
{% endif %}
{% endif %}
{% endfor %}
<p class="submit">
<input type="submit" name="post" class="submit-post" value="{% trans "Post" %}" />
</p>
</form>
After submitting the form, it redirects to http://127.0.0.1:8000/comments/posted/?c=..
That means it calls the template django/contrib/comments/templates/comments/posted.html
The content of django/contrib/comments/templates/comments/posted.html:
{% extends "comments/base.html" %}
{% load i18n %}
{% block title %}{% trans "Thanks for commenting" %}.{% endblock %}
{% block content %}
<h1>{% trans "Thank you for your comment" %}.</h1>
{% endblock %}
That doesn't extends my project's base.html.
I need to customize/override that template so that it extends my project's base.html. How can i do that?
If i can't do that, then if i upload my django web project on server, then how would i edit the content of django/contrib/comments/templates/comments/posted.html so that it looks like that:
{% extends "path/to/myproject/templates/base.html" %}
{% load i18n %}
{% block title %}{% trans "Thanks for commenting" %}.{% endblock %}
{% block content %}
<h1>{% trans "Thank you for your comment" %}.</h1>
{% endblock %}
On local pc, for this time i changed/edited the content of django/contrib/comments/templates/comments/posted.html hard-coded to extends my project base.html.
Can anyone give some idea to solve this? I have searched a lot to solve this.
Just override it in your project's "templates" directory:
<project_root>/templates/comments/posted.html
It doesn't seem to be well documented in either the comments app or Django's general template documentation, but it works the same as overriding admin templates (which is documented).
i'm using django-haystack with simple engine, the search is fine, but the pagination is not working. This is the code of my search results template
{% if query %}
<br>
<div id="contenido_pagina">
{% for result in page.object_list %}
{% if result.object.get_model == '[Video] ' %}
{% if result.object.publicar %}
<div class="salida_search">
{{result.object.get_model}}{{result.object.nombre}}<br>
<div class="resumen_search">
{{result.object.sinopsis|safe|truncatewords:"30"}}
</div>
<div class="link_search">
{{result.object.anio}}
</div>
</div>
{% endif %}
{% else %}
<div class="salida_search">
{{result.object.get_model}}{{result.object.titulo}}<br>
<div class="resumen_search">
{% if result.object.contenido %}
{{result.object.contenido|safe|truncatewords:"30"}}
{% else %}
{{result.object.sinopsis|safe|truncatewords:"30"}}
{% endif %}
</div>
<div class="link_search">
{{result.object.fecha|date:"d M Y"}}
</div>
</div>
{% endif %}
{% empty %}
<div>La busqueda <span class="highlighted">{{query}}</span> no obtuvo ningun resultado</div>
{% endfor %}
{% if page.has_previous or page.has_next %}
<div>
{% if page.has_previous %}{% endif %}« Anterior{% if page.has_previous %}{% endif %}
|
{% if page.has_next %}{% endif %}Siguiente »{% if page.has_next %}{% endif %}
</div>
{% endif %}
</div>
<br>
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
when i go to the next page i see the same objects that first page. What i am doing wrong??
I had this issue as well. From debugging the code it looks like it's an issue with the paginator and the searchqueryset. Unfortunately i didn't have more time to devote to it and ended up moving on to whoosh for development environment.
Just encountered this. Seems as though it's a known limitation of the simple backend.
https://github.com/toastdriven/django-haystack/issues/320