My menu in the html looks like this:
<ul id="nav-mobile" class="right">
{% user_display user %}
<li>Login</li>
<li>Logout</li>
<li>Signup</li>
</ul>
Obivous this doesn't make much sense, cause if a user is not logged in it should be possible to logout and vice versa.
Are there any template-tags in django or django-allauth, which I can write in the template like. PseudoCode
<if user login>
<a> logout </a>
<end if>
You can simply check if user is logged in with is_authenticated in your template like this:
{% if request.user.is_authenticated %}
{% user_display user %}
<li>Logout</li>
{% else %}
<li>Login</li>
<li>Signup</li>
{% endif %}
Related
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 %}
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
On my html, I can check if the user is logged in by using the following syntax:
{% if user.is_authenticated %}
<div id="display_something">...</div>
{% else %}
<p>Please Log in</p>
{% endif %}
But what should I do if I want to check if the user is authenticated for every html file I am rendering? Do I have to copy and paste that {% if ... %} block for every single html file? What is the Django's way of handling this issue? What's the good practice?
in your base.html, add your check
{% if user.is_authenticated %}
{% block page %}
{% endblock %}
{% else %}
<p>Please Log in</p>
{% endif %}
then with all your other pages, add {% extends 'base.html' %} at the top. You will need to give it a relative link to base.html.
Then the rest of your code on that page needs to sit between tags like below.
{% block page %}
<!-- all your html code here -->
{% endblock %}
Notice that after block, you need to have the same name. for this example, it is page but you can pick your own variable name.
You shouldn't handle the authentication logic in the template (for the entire site), instead you can use the login_required decorator on your views.
from django.contrib.auth.decorators import login_required
#login_required
def my_view(request):
...
I have an issue with my application I'm building. I understand how to validate if a user in django is logged in or not and whether their session is active with:
if user is not None and user.is_active:
My issue is my django templates specifically with the section with Register | Login that look like:
<div id="subnav_registrationLogin">
<ul>
{% block block_containersupernav %}
<li><span>Register</span></li>
<li style="border:none;"><span>Login</span></li>
{% endblock block_containersupernav %}
</ul>
</div><!-- /subnav_registrationLogin -->
The issue is, my template is static and for this little code snippet above needs be more dynamic like:
if user is not None and user.is_active:
Log Out
elif:
<div id="subnav_registrationLogin">
<ul>
{% block block_containersupernav %}
<li><span>Register</span></li>
<li style="border:none;"><span>Login</span></li>
{% endblock block_containersupernav %}
</ul>
</div><!-- /subnav_registrationLogin -->
How can I achieve this within a template? and if I cannot within a template, how should I be doing this? Thank you!
Templates are rendered based on the context. So try this:
{% if user.is_authenticated %}
Logout
{% else %}
<div id="subnav_registrationLogin">
<ul>
{% block block_containersupernav %}
<li><span>Register</span></li>
<li style="border:none;"><span>Login</span></li>
{% endblock block_containersupernav %}
</ul>
</div>
{% endif %}
is_authenticated() is a helper method in the django.contrib.auth.user model
Also, note that is_active flag is used to check if the user is active or not, and should be used to check if the user can be successfully logged into the system or not.
You could also access the current logged in user using request.user.is_authenticated in the template.
So something like this?
{% if user.is_authenticated %}
<li>Logout</li>
{% else %}
<div id="subnav_registrationLogin">
<ul>
{% block block_containersupernav %}
<li><span>Register</span></li>
<li style="border:none;"><span>Login</span></li>
{% endblock block_containersupernav %}
</ul>
</div><!-- /subnav_registrationLogin -->
{% endif %}
I'm having some trouble with Django's authentication system. I've managed to set up a login page, logout page and a basic profile page. Now I'm trying to restrict different areas on the site to only authenticated users. It works on some templates, and not on others.
The weirdest, maybe, is that it works/not works in the same template.
This is base.html:
<div id="account">
{% if user.is_authenticated %}
Hello, {{ user.username }}! | Log out
{% else %}
Log in
or
Sign up
{% endif %}
</div>
<div id="sidebar">
{% if user.is_authenticated %}
<h3 id="plus" style="padding-top: 20px;">Sign up!</h3>
Log in
{% else %}
<div style="margin-top: 45px">
Profile
</div>
{% endif %}
</div>
Works in the account-div, but not in the sidebar-div.
Any suggestions?
You have
{% if user.is_authenticated %}
<h3 id="plus" style="padding-top: 20px;">Sign up!</h3>
Log in
{% else %}
why does he have to sign up if he's logged in?
You can try {% if not user.is_authenticated %}