Django checking for a query string - python

When someone clicks on my site's logout link, I want them to be redirected to the login page but I want a message to pop up saying that they were successfully logged out. However, I don't want this behaviour to occur when someone normally visits the login page.
I decided to pass in a query string so now when someone hits logout they are redirected to users/login/?logout. How can I check for this in my template though? I want to do something like:
{% if ______ %}
*Message box appears*
{% endif %}
Thanks!

Well, you can simply use the built-in Django messaging feature!
Here's a simple example:
# views.py
from django.contrib import messages
def register(request):
# Logic
# ...
messages.success(request, 'Registered successfully!')
# ...
# More logic
-
# example.html
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li class="{{ message.tags }}">
{{ message|safe }}
X
</li>
{% endfor %}
</ul>
{% endif %}
There's a more in-depth documentation available on Django's official docs website: https://docs.djangoproject.com/en/1.11/ref/contrib/messages/

Referring to this document, I believe what you are looking for is this
{% if not request.user.is_authenticated %}
<p>You are logged out!</p>
{% endif %}

Related

Django get user data in main template

I'm trying to check if user logged in in the main template using this:
{%if request.user%}
...
{%endif%}
but it's not working maybe because main template doesn't have a view
could any one help i don't know if the question duplicated,but i didn't find my answer.
you could use:
{% if user.is_authenticated %}
...
{% endif %}
{% if user.is_authenticated %}
...
{% endif %}
Link to the Django docs. this should always be your first point of reference.

Create pop up message in django

I am doing some calculations in my views.py and I want to show the result.
I have tried to use the following:
return render(request, index.html{message:variable})
and
return HttpResponse(message)
I want to show the result as a Popup message. How can I implement this?
first you have to render template by
return render_template( "example.html",message = any_variable)
and then you can show pop up on html via java script by
<script>
alert("This is alert box!"); // display string message
alert( {{message}} ); // display python variable
</script>
although i recommend using jinja as template
You can use Django messages https://docs.djangoproject.com/en/3.2/ref/contrib/messages/
from django.contrib import messages
messages.success(request, 'Success')
In your template, use something like:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}

How to send message to HttpResponseRedirect in Django

I have added a middleware to a session-out user with my logic. I am trying to redirect to login page with a message. I am using HttpResponseRedirect(reverse('user_login')) for redirecting to my custom login page.
So, how to pass values to a page?
You can use django's messages framework. https://docs.djangoproject.com/en/stable/ref/contrib/messages/
In your views do:
from django.contrib import messages
messages.success(request, 'This is my message.')
And in your base template keep this piece of code.
{% if messages %}<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}</ul>{% endif %}
Or if don't want to use messages you can pass in the url.
from django.core.urlresolvers import reverse
return HttpResponseRedirect(reverse('user_login') + '?autologout=true')
And in your template
{% if request.GET.autologout %}
<p>This is my message.</p>
{% endif %}

Using Django Templates with User Authentication

I have a site which I am using user authentication to limit access to.
I am using django-allauth to handle social media authentication, but want to limit access to staff accounts only (set in the django admin panel).
I want to use a template as a header (bootstrap, nav bar etc.) with the main content loaded afterwards, or a message asking the user to login or request to be verified by admins.
My header template file: inventory/header.html
<body>
{% load bootstrap3 %}
{% if user.is_authenticated %}
{% if user.is_staff%}
{% block main %}{% endblock %}
{% block scripts %}{% endblock %}
{% else %}
<h1>Please ask an administrator to activate your account</h1>
{% endif %}
{% else %}
<h1>Please login to see this page</h1>
{% endif %}
</body>
And another template called by the view: inventory/home.html
{% include "inventory/header.html" %}
{% block main %}
This is the home page.
{% endblock %}
The view:
def home(request):
return render(request,'inventory/home.html')
Whenever I call the view, regardless of whether the user is logged in or a staff member, the main block is always displayed. The other error messages are displayed correctly.
I'm trying to avoid inserting is_authenticated/is_staff into all of my templates. I have also thought about using the login_required view decorator but that does not solve my is_staff issue.
Turns out I should have used
{% extends "inventory/header.html" %}
instead of
{% include "inventory/header.html" %}
Doh!

Unable to retrieve user information from django templates

i want to use loged in user information in my base template
{% if request.user.username %}
Logout
{% else %}
Login
{% endif %}
from django.template import RequestContext
def top(request):
return render_to_response('top.html',{}, context_instance=RequestContext(request))
If you're just wanting to check if they're logged in to display the login or logout links, I would use is_authenticated instead
{% if user.is_authenticated %}
Logout
{% else %}
Login
{% endif %}
If you are wanting to use information from the user, you can use the variables in the user class {{ user.username }}
{% if user.is_authenticated %}
Hi, {{ user.first_name }}, Logout
{% else %}
Login
{% endif %}
I use code with this structure in my base.html and it works well. I don't have anything in the template_content_processor - it works in the template on its own.

Categories

Resources