Django get user data in main template - python

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.

Related

Django - The page don't load after click

i'm new in Django developing.
I'm following the tutorial about Library on MDN (https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django)
Until i follow the code all work but i'm trying implement author page by myself. Probably is very stupid issue but is one day that i'm turning around like a dog with its tail.
There is 2 page: author_list and author detail.
I set urls.py (in my project) i set view.py and crate my template.
I follow the same step of tutorial for realize book_list and book_detail but when i click on my author the page don't go to the detail of that author and stay in author_list.html.
Here the code urls.py :
path('authors/', views.AuthorListView.as_view(), name='authors'),
path('author/<int:pk>', views.AuthorDetailView.as_view(), name='author-detail'),
Here views.py:
class AuthorListView(generic.ListView):
model = Author
class AuthorDetailView(generic.ListView):
model = Author
Here author_list.html with link get_absolute_url:
{% extends "base_generic.html"%}
{% block content %}
<h1>Author list</h1>
{% if author_list %}
<ul>
{% for aut in author_list %}
<li>{{ aut.first_name }} - {{ aut.last_name }}</li>
{% endfor %}
</ul>
{% else %}
<p>There are no author.</p>
{% endif %}
{% endblock %}
Here author_detail.html:
{% extends "base_generic.html" %}
{% block content %}
<h1>Author</h1>
{% if author %}
<p><strong>Nome: </strong> {{ author }}</p>
<p><strong>Nato il : </strong> {{ author.date_of_birth }}</p>
<p><strong>Morto il : </strong> {{ author.date_of_death }}</p>
{% endif %}
{% endblock %}
Here the screenshot
Author_list.html before click url=catalog/authors/
After click url change but page not
Thank to all for help
I believe you need DetailView instead of ListView for AuthorDetailView.
Looks like a typo to me, you want generic.DetailView (instead of ListView) for the author/<int:pk> path.
I also don't think it's right to extend base_generic for the template for the detail view. But that depends exactly what is in this base template.

Django checking for a query string

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

Python template include a page if condition

i'm developing a small app with Python and Google app engine. I'm using boilerplate (https://github.com/coto/gae-boilerplate) as front-end which follows gae direction and python templates, so nothing diffrent than plain stuff.
Now, what i would like to have is this.
When a user logged in, if the field of name and last name are not filled in i would like to have, in the home page, the profile editing.
The page for editing the profile is a template (which extend the base.html), called edit_profile.html which works well.
The homepage is a template as well (extend the base.html) called home.html.
Now, can i include the edit_profile.html in home.html? how can i do it?
this is what i've, i don't know what to put instead of ???? i tried with
{% block edit_profile.html %} {% endblock %}
but does not work
{% if user_info.name and user_info.last_name %}
..
{% else %}
????
{% endif %}
thanks.
So you want to include only some block of given template. There are two solutions:
1) Create template just for profile editing form and include it into edit_profile.html. Then include it also into home.html to if condition branch:
profile_form.html:
<form action="{% url some-action %}">
{{ form }}
<input type="submit" value="save"/>
</form
profile_edit.html
{% extends "base.html" %}
{% block main %}
{% include "profile_form.html" %}
{% endblock %}
home.html
{% if user_info.name and user_info.last_name %}
{% include "profile_form.html" %}
{% endif %}
2) use variable for extended template:
profile_form.html
{% extend BASE_TEMPLATE %}
and set it into context w/ different value as needed:
in home.html (let's say included_form.html is some basic template)
{% if user_info.name and user_info.last_name %}
{% with "included_form.html" as BASE_TEMPLATE %}
{% include "edit_profile.html" %}
{% endwith %}
{% endif %}
and if you want show form as a standalone page, set BASE_TEMPLATE to base.html

Control flow in Django template language

I have the following code, written inside a Django template.
{% if user.is_authenticated %}
<div style="float: right;">
{% for rel in RELATIONS %}
{% if rel.group_id == g.id %}
Unsubscribe
{% endif %}
{% else %}
Enrol
{% endfor %}
</div>
{% endif %}
The purpose of the code is to check if there is a match and then print out the unsubscribe tag. If there is not match print out the subscribe tag.
The reason I am having trouble doing this is because you in Django templates, I read that you can't have variables (i.e. a True or False).
UPDATE: (The question)
I want to only print out the Unsubscribe/subscribe button once. Print out the unsubscribe button only if there is a match inside the for loop. Otherwise print out the subscribe button if there is no match (i.e. no match at all for rel.group_id == g.id )
UPDATE 2:
While doing some research earlier I found this: https://code.djangoproject.com/ticket/3481
This might add some context to my problem.
Thank you for your help.
Seems like your if/else/endif are mixed up. Try
{% if user.is_authenticated %}
<div style="float: right;">
{% for rel in RELATIONS %}
{% if rel.group_id == g.id %}
Unsubscribe
{% else %}
Enrol
{% endif %}
{% endfor %}
</div>
{% endif %}
UPDATE
You want to check if g is in relations and make your decission based upon that.
The way I usually solve this is to create a function in my view that does this check and passes that allong to the view. Have a look here.

Django query manytomanyfield in template

How can I query a manytomanyfield in a Django template?
For example, this if statement doesn't work (I know I can't call functions with arguments in Django templates), but this shows what I'd like to do:
template.html
{% for post in posts %}
{% if post.likes.filter(user=user) %}
You like this post
{% else %}
<a>Click here to like this post</a>
{% endif %}
{% endfor %}
models.py
class User(Model):
# fields
class Post(Model):
likes = ManyToManyField(User)
In order to do what you are looking for, you could do the following:
{% for post in posts %}
{% if user in post.likes.distinct %}
You like this post
{% else %}
<a>Click here to like this post</a>
{% endif %}
{% endfor %}
Alternatively, you could use Greg's approach. The advantage of his answer is that it would scale better when you get into very large datasets. This approach does not require you to write any custom filters.
It doesn't work because you appear to be writing python code in a template... you need to either run the loop in your view and pass a list of posts and their information to the template, or write a template filter that determines whether a certain user likes a post. For example:
from django import template
register = template.Library()
#register.filter
def is_liked_by(post, user):
return bool(post.likes.filter(user=user))
Then in your template:
{% for post in posts %}
{% if post|is_liked_by:request.user %}
You like this post
{% else %}
<a>Click here to like this post</a>
{% endif %}
{% endfor %}

Categories

Resources