Make the userpicture a link - python

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.

Related

Reverse for 'surveydetails' not found. 'surveydetails' is not a valid view function or pattern name

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 %}

Access model properties in Django Admin templates?

I have Django 2.0.3 on Python 3.5.3. I search for simple way to small improve my standard Django Admin dashboard (main page of Django Admin).
Here is my template for Admin main page ./templates/admin/index.html:
{% extends "admin/base_site.html" %}
{% load i18n static %}
{% block extrastyle %}{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static "admin/css/dashboard.css" %}"/>{% endblock %}
{% block coltype %}colMS{% endblock %}
{% block bodyclass %}{{ block.super }} dashboard{% endblock %}
{% block breadcrumbs %}{% endblock %}
{% block content %}
<div id="content-main">
{% if app_list %}
{% for app in app_list %}
<div class="app-{{ app.app_label }} module">
<table>
<caption>
<a href="{{ app.app_url }}" class="section"
title="{% blocktrans with name=app.name %}Models in the {{ name }} application{% endblocktrans %}">{{ app.name }}</a>
</caption>
{% for model in app.models %}
<tr class="model-{{ model.object_name|lower }}">
{% if model.admin_url %}
<th scope="row">{{ model.name }}</th>
{% else %}
<th scope="row">{{ model.name }}</th>
{% endif %}
{% if model.add_url and request.user.is_superuser %}
<td>{% trans 'Add' %}</td>
{% else %}
<td> </td>
{% endif %}
{% if model.admin_url and request.user.is_superuser %}
<td>{% trans 'Change' %}</td>
{% else %}
<td> </td>
{% endif %}
</tr>
{% endfor %}
</table>
</div>
{% endfor %}
{% else %}
<p>{% trans "You don't have permission to edit anything." %}</p>
{% endif %}
</div>
{% endblock %}
I delete sidebar block, because I never use it. And it looks like:
I want to add to each model link count of objects. For example, Cities (23), Citizenships (102) and similar for all models in list. I try to add function with #property decorator in ./app/models.py, but it's not working:
class APIConfig(models.Model):
...
#property
def with_active_status(self):
return self.objects.filter(is_active=True).count()
...
I call this property in Admin template, like {{ model.with_active_status }} and it shows nothing.
This can't be a property. You don't have an instance of the model to call it on. In any case, if you did have an instance it still wouldn't work, as model managers - objects - can only be accessed from the class, not the instance.
You should make it a classmethod:
#classmethod
def with_active_status(cls):
return cls.objects.filter(is_active=True).count()

Error in the template with Django : can I make arithmetic in if statement

I would like to make a pagination with Bootstrap : a new page every 10 new field in data.
file.html
{% for d in data %}
{% if forloop.first %}
<ul class="pagination">
{% endif %}
{% if (forloop.counter % 10) == 0 %}
<li>{{ forloop.counter % 10 }}</li>
{% endif %}
{% if forloop.last %}
</ul>
{% endif %}
{% endfor %}
output I would like that => Bootstrap pagination
But Django give me an error for this :
{% if (forloop.counter % 10) == 0 %}
TemplateSyntaxError :/
I don't know how to do except create my own filter or add a filter, but i would like to know first if i can do in the template first.
PS: I use Django 1.5 and I can't upgrade it.
Edit:
Finally I use this condition:
{% if forloop.counter|divisibleby:'10' and forloop.counter|divisibleby:'5' and forloop.counter|divisibleby:'2' %}
Like that I know when I have a 10 multiple.
The modulus (%) operator is not available in django templates. However, you can use the divisibleby (https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#divisibleby) template filter, something like
{% if forloop.counter|divisibleby:"2" %}
Use the paginator, your QuerySet are not evaluated for the hole table, just the number you need to build the page, and it offers properties that you can use in the template like (page_range, next_page_number, has_next, etc.)
here is the code withe BootStrap 2 and django.core.paginator:
<div class="pagination pagination-centered">
<ul>
{% if MYDATAENTIRES.has_previous %}
<li>
{% trans "Précédent" %}
</li>
{% endif %}
{% for i in MYDATAENTIRES.paginator.page_range %}
<li {% ifequal MYDATAENTIRES.number i %} {{ 'class="disabled"' }} {% endifequal %}>
<a href="?page={{ i }}">
{{ i }}
</a>
</li>
{% endfor %}
{% if MYDATAENTIRES.has_next %}
<li>
{% trans "Suivant" %}
</li>
{% endif %}
</ul>
</div>

How can i override django contrib comments template?

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).

django "load comment"s not working correctly

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!

Categories