Get the user context in all views django? - django

Im using a template syntax to set the permissions for the topbar of the application like so
Top bar permision
{% if user.is_authenticated %}
# Top bar html code
{% endif %}
but the problem is that when a user i authenticated they can accress the profile picture and name of another user if they have the correct url:uuid link.
I managed to get this fixed by putting this syntax in the template
{% if object == request.user %}
{% if object.pk %}
{% endif %}
{% endif %}
This fixed the issue on the pages where the request.user can be accressed from the profile view. How can i accress the request.user in all the views at once?

Solved the problem i moved the templates tags to this order
{% if user.is_authenticated %}
{% if user == request.user %}
{% if user.pk %} {% endif %}
{% endif %}
{% endif %}

Related

How to apply multiple if condition filter in djago template

I am rendering two filter from my views to html template. 1) notifications_approved 2)notifications_pending. I am using this two filter separately in my html. Is there any way to use this two filter together in if statement? here is my code:
#html template
{% for notification in notifications_approved %}
{% endif %}
{% for notification in notifications_pending %}
{%endif%}
I tried this but it's not rendering anything's in my template:
{%if notification in notifications_approved or notifications_pending %}
{%endif%}
I also tried this
{%if notification|notifications_approved or notification|notifications_pending %}
{%endif%} #getting TemplateSyntaxError
I also surprised very much why this if statement not working:
{% if notification in notifications_approved %}
{% endif %}
views.py
def ShowAuthorNOtifications(request):
notifications_approved = Notifications.objects.filter(notification_type="Comment Approved").order_by('-date')
notifications_pending = Notifications.objects.filter(notification_type="New Comment").order_by('-date')
{% if notification in notifications_approved %}
{% endif %}
If statement not working because we are not comparing anything. notifications_approved is a query set which contains list of objects. You need to loop it first. Secondly, if you need to check if the query set is empty or not, use:
{% with notifications_approved as np %}
{% if np %}
# your code
{% endif %}
{% endwith %}
OR
{% if notifications_approved or notifications_pending %}
OR
{% if notifications_approved.exists or notifications_pending.exists %}
OR
{% if notifications_approved.all or notifications_pending.all %}
You can use empty tag as well to check if empty:
{% for na in notifications_approved %}
# code
{% empty %}
# code for empty query set
{% endfor %}

django template tag check if image exists

I've got a portion of my app that allows a custom logo.
Default object (and object view) state is no custom logo, but if one exists, replace the header with a custom logo navbar.
logo is a standard ImageField on co(mpany) model:
models.py:
class Co(models.Model):
logo = models.ImageField(blank=True)
template:
{% if co.logo %}
{% block navbar %}
{% include 'templates/navbar_logo.html' %}
{% endblock %}
{% endif %}
I've also tried this with {% if co.logo == None %}, {% if not ... %}, {% if co.logo.url %} and trying to model the logic with co.logo|default_if_none:"" but in instances where a logo isn't set the view throws:
ValueError at /co/foo
The 'logo' attribute has no file associated with it.
for... empty also doesn't work
{% for co.logo.url in co %}
...
{% empty %}
...
{% endfor %}
in django shell:
(with logo)
c.logo >> <ImageFieldFile: logo.png>
c.logo.url >> '/media/logo.png'
(no logo)
b.logo >> <ImageFieldFile: None>
is there a built in django template tag filter that will allow this condition to pass True only if the field has an image uploaded? otherwise don't load the nav block? thanks
I solved this by re-ordering the tags:
{% block navbar %}
{% if co.logo %}
{% include 'templates/navbar_logo.html' %}
{% else %}
{% include 'templates/navbar.html' %}
{% endif %}
{% endblock %}
Try Use 'is not null'
{% if co.logo.url is not null %}
{% block navbar %}
{% include 'templates/navbar_logo.html' %}
{% endblock %}
{% endif %}

Django: how to pass value from a list template to a view, static url

In my template, I have a list of users on which the connected user can click to access the profile of a user on the list. I want to retrieve the user I clicked on in my view. However, I would like to keep a static URL (to avoid having user-related settings displayed in the URL). Do you have any idea how to do that?
Here is my template :
{% extends "base.html" %}
{% block content %}
{% if userConnected.adminRole == "SuperUser" %}
<h2>Members List</h2>
<h3>Members from OU : {{ userConnected.ou }} </h3>
{% for element in user_list %}
<p>{{ element.first_name }}</p>
{% endfor %}
{% endif %}
{% endblock %}
What should I add to my template and what should I write in my view "edit_other_profile" to get the right user ? Thanks a lot

Removing the logged in user's name from the list of users in django

I have a template that displays the numbers of likes and name of the users liked the forum. But I don't want the request.user's (logged in user's) name in the list, I just want the other user's name and not the user itself. How do I achieve this? And also I want the list of the names in the reverse order in the template (now its showing the latest liked user in the last of the list.). Please guide me. Thank you.
forums.html:
{% extends "base.html" %}
{% load forum_tags %}
{% block content %}
<h2>Logged in as -- {{request.user}}</h2>
<h1>Forums:</h1>
{% if forums.count > 0 %}
{% for forum in forums %}
<h2>{{forum.question}}</h2>
<p>{{forum.body | truncatewords:"30"}}</p>
{% if user in forum.likes.all and forum.likes.count > 1 %}
<p>Unlike You and {{forum.likes.count | substract:1}} others liked</p>
{% elif user in forum.likes.all %}
<p>You liked it</p>
{% else %}
<p>Like</p>
{% endif %}
{% for likes in forum.likes.all %}
<li>{{likes.get_full_name}}</li>
{% endfor %}
{% endfor %}
{% else %}
<p>Sorry! No forum to display.</p>
{% endif %}
{% endblock %}
snippet of views.py:
def forums(request):
forums = Forum.objects.all()
c = {'forums': forums}
return render(request, 'forums.html', c)
Use RequestContext() while passing context to template. It will add user context parameter , then you can compare it in your template as
{% for likes in forum.likes.all %}
{% if user != likes.user %}
<li>{{likes.get_full_name}}</li>
{%endif%}
{% endfor %}
Can you try something like that ?
{% for likes in forum.likes.all %}
{% if user != likes.user %}
<li>{{likes.get_full_name}}</li>
{% endif %}
{% endfor %}
I had to compare it with fullname, as I was displaying the users full name in the list. So, this was the solution:
{% for likes in forum.likes.all.reverse %}
{% if user.get_full_name != likes.get_full_name %}
<li>{{likes.get_full_name}}</li>
{% endif %}
{% endfor %}
Hope this will be helpful to someone as it was for me. And thanks for all those who helped me!

How to redirect back to same page when errors in Django comments

How do you get Django comments to redirect back to the same page where you're filling out a comment if there are errors in the comment submission form?
So basically I have a template like this:
{% block content %}
{% render_comment_form for show %}
{% get_comment_count for show as comment_count %}
<div id="comments-count">
{% if comment_count == 0 %}
No comments yet. Be the first!
{% else %}
Number Of Comments: {{ comment_count }}
{% endif %}
</div>
{% if comment_count > 0 %}
{% render_comment_list for show %}
{% endif %}
{% endblock %}
I created my own list.html and form.html and everything looks fine. In the form.html template there is some code like this:
<ul class="form-errors">
{% for field in form %}
{% for error in field.errors %}
<li>{{ field.label }}: {{ error|escape }}</li>
{% endfor %}
{% endfor %}
</ul>
So obviously, if there is an error in the comment submission form, I would like the user to see the same page as before only with some errors displayed in the comments form. Or alternatively if this is not possible, just ignore the error and instead of transitioning to the preview.html template, it would just not save the comment and again go back to the page.
Any help? Note ideally I dont want to have to create a custom comments app. This functionality should be there already. I know there's a next variable you can pass (and I am doing this), but it only works if the comment form is successful.
you have to use HttpResponseRedirect
from django.http import HttpResponseRedirect
def comment_form(request):
error = request.GET.get('error', None)
requestDict = {'error': error}
return render_to_response('comments.html', requestDict, context_instance=RequestContext(request))
def post_comment(request):
....
your code
....
if something_goes_wrong:
HttpResponseRedirect('project/comment_form/?error=ThereisProblem')
And in template you can do this:
{If error %}
<h1>{{error}}<h1>
{%else%}
render comments...
{%endif%}
Hope this will help you :)