Django authentication acting weird. Working "some times", not always - django

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

Related

Is there a way to hide a button on every page except Home page of a site in Django?

<div class="page-header">
{% if user.is_authenticated %}
<span class="glyphicon glyphicon-plus"></span>
{% endif %}
<h1>Django Girls Blog</h1>
{% if user.is_anonymous %}
<h4>LogIn</h4>
{% endif %}
{% if user.is_authenticated %}
<span class="glyphicon glyphicon-edit"></span>
{% endif %}
{% if user.is_authenticated %}
<h3>{{ user }}</h3>
<h4>LogOut></h4>
{% endif %}
Absolutely. Check if the current page is the home page and if it is not then hide the button with css. Like so:
<button {% if request.path != '/' %} style="display: none;" {% endif %}>My Button</button>

Querysets "lost" when I render them in the template

I was stucked the last two days with this problem. First part of the code:
viewa.py
def A_dashboard(request):
user = User.objects.get(id=request.user.id)
users = User.objects.all()
return render_to_response('dashboard.html', {"user": user, "users": users} , context_instance=RequestContext(request))
The dashboard will be different depending of one property of the user.
dashboard.html
{% extends "index.html" %}
{% load staticfiles %}
{% block content %}
{% if user.is_A %}
{% include "dashboards/A_dashboard.html" %}
{% endif %}
{% if user.is_B %}
{% include "dashboards/B_dashboard.html" %}
{% endif %}
{% endblock content %}
The concrete dashboard to the A user.
dashboards/A_dashboard.html
{% load staticfiles %}
<div class="offrow rowpadding dashboard-info">
<div class="container">
<div class="row">
<div class="col-lg-7">
<div class="row">
{{user}}
{{users}}
{% for user_ in users %}
<p>{{user_}}</p>
{% endfor %}
</div>
</div>
<div class="col-lg-5">
<div class="row">
<div class="col-md-12">
{% include 'calendar.html' %}
</div>
</div>
</div>
</div>
</div>
Right Now Im logged in as an A user. In the view I check the class to check that everything is ok. Its of class <class 'django.db.models.query.QuerySet'>
However, when I try to iterate the list in the template, it doesn't exist.
Can you imagine whats happening?
Regards.
it is strange that objects don't exist.
User.objects.all() is queryset, so in template users is a queryset.
user is a user object, it has some attributes, such as username , email and so on.
for user_ in users you get every user object(user_) similar to user above
so try this
<div class="row">
name: {{ user.username }}
{% for user_ in users %}
<p>name: {{ user_.username }}</p>
{% endfor %}
</div>
{{user.username}} and {{ user.username }} are the same, spaces just for looking elegant

Issue building Jekyll blog post page and pagenation

I was adding pagination to my website tonight and I ran into issues where now I have broke my whole site.
I am using the standard Jekyll directory structure for the files. For my website, I want the index page to be about me, then you click the /blog page to view my blog. So I have index.md be by main page. Then I have a page blog.html be the blog page. I used to use {% for post in site.posts limit: 10 %} which worked and switched to {% for post in paginator.posts %} based on how the latest documentation recommended the loop and it broke. In my _config.yml file I have:
paginate: 10
paginate_path: "blog/page:num"
permalink: /blog/:year/:month/:day/:title.html
I want the paging to be /blog/page2.html. I am unsure what I have configured wrong. Here is my entire blog.html page:
---
title: Blog
layout: default
permalink: /blog/index.html
---
{% for post in paginator.posts %}
<div class="row">
<div class="col-lg-12">
{% if post.layout contains "link" %}
<h4><i class="icon-link"></i> {{post.title}}</h4>
{% else %}
<h4>{{post.title}}</h4>
{% endif %}
<small>{{post.date | date: "%m/%d/%Y"}}</small>
<div class="post-content-truncate">
{% if post.content contains "<!-- more -->" %}
{{ post.content | split:"<!-- more -->" | first % }}
Read full article...
{% else %}
{{ post.content }}
{% endif %}
</div>
</div>
</div>
{% endfor %}
{% if paginator.total_pages > 1 %}
<div class="row">
<div class="col-lg-12">
<ul class="pagination">
{% if paginator.previous_page %}
<li>« Prev</li>
{% else %}
<span>« Prev</span>
{% endif %}
{% for page in (1..paginator.total_pages) %}
{% if page == paginator.page %}
<li><em>{{ page }}</em></li>
{% elsif page == 1 %}
<li>{{ page }}</li>
{% else %}
<li>{{ page }}</li>
{% endif %}
{% endfor %}
{% if paginator.next_page %}
<li>Next »</li>
{% else %}
<li><span>Next »</span></li>
{% endif %}
</ul>
</div>
</div>
{% endif %}
Any ideas of where I am going wrong?
The problem was actually two problems:
I was using an older version of Jekyll. Once I upgraded to the latest it was almost working.
I had to put my /blog.html file into /blog/index.html
Once I did those two steps it worked.

User Authentication based on if logged in or not django

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

django - Invalid block tag: 'add_pinned_status', expected 'else' or 'endif'

I get the following error when serving my django application using Nginx+FastCGI
Invalid block tag: 'add_pinned_status', expected 'else' or 'endif'
Oddly, the site works just fine when I'm serving using the Django development server. It also works with Nginx most of the time, but the error randomly appears and reappears with refreshes. Any idea what the problem could be?
EDIT: Here's the code, just to clarify that there's NO hanging if statement.
{% extends 'master.html'%}
{% load thumbnail %}
{% load tags %}
{% block 'title' %}
{{ title }}
{% endblock %}
{% block 'content' %}
<div id="feed" class="content">
{% for book in books.object_list %}
<div class="book_preview">
<div class="thumbnail">
<a href="/book/{{ book.id }}/{{ book.get_slug }}/">
{% if book.cover_image %}
{% thumbnail book.cover_image "120" as im %}
<img src="{{ im.url }}" alt="Python for Software Design"/>
{% endthumbnail %}
{% else %}
<img src="{{ STATIC_URL }}default_thumb.jpg" alt="Python for Software Design"/>
{% endif %}
</a>
</div>
<div class="book_details">
<h2 class="book_title">
<a class="book_profile_link" href="/book/{{ book.id }}/{{ book.get_slug }}/">{{ book.title }}</a>
{% if user != book.uploader %}
<a class="shelf_adder {% add_pinned_status request book.pk %}" href="/shelf/{{ book.id }}/toggle/?next={{ request.get_full_path }}" title="Toggle shelf status"></a>
{% endif %}
</h2>
<h3 class="book_subtitle">
{% if book.subtitle %}
{{ book.subtitle }}
{% else %}
<a href='/book/{{book.id}}/edit/#subtitle'>Provide subtitle</a>
{% endif %}
</h3>
<h3 class="book_authors"> by {{ book.author.filter|join:", " }}</h3>
<div class="book_description">
{% if book.description %}
<p>
{{ book.description|truncatewords:25 }}
</p>
{% else %}
<p class="message">No description available. Create one.</p>
{% endif %}
</div>
<div class="book_links">
<a href="/book/{{ book.id }}/{{ book.get_slug }}/" class="book_profile_link" title="Book profile">
Book profile
</a>
<a href="http://{{ book.homepage }}" class="book_website_link" title="Book website" target="_blank">
Book website
</a>
</div>
<p>Points: {{ book.shelf_additions }}</p>
<div class="book_tags">
{% if book.topics.all %}
{% for topic in book.topics.filter %}
{{ topic }}
{% endfor %}
{% else %}
<a href="/book/{{ book.id }}/edit/#topics" title='Click to add'>no topics added☹</a>
{% endif %}
</div>
</div>
<div style="clear: both;"></div>
</div>
{% endfor %}
<div class="pagination">
{% if books.has_previous %}
previous
{% endif %}
<span class="current">
Page {{ books.number }} of {{ books.paginator.num_pages }}
</span>
{% if books.has_next %}
next
{% endif %}
</div>
</div>
{% endblock %}
The problem starts on the line after the if user != book.uploader statement, which as you can see is terminated with the appropriate endif. I suspect it may be some sort of timeout but I'm not entirely sure. Keep in mind, it works sometimes but randomly stops when using Nginx. It works flawlessly with the dev server.
Django gives that error when you have an unclosed templatetag. In this case an {% if ... %} templatetag.
As to why it only happens in certain scenarios, it might be inside a conditional tag itself, so it's not always processed, but I think Django processes the whole template despite what's going on conditionally or not. It might also be possible that there was some mistake in updating your production site and it's using a different/older version than your development site.
Regardless, the error is the error. Find the unclosed templatetag, and you'll solve it across the board.
UPDATE: The alternative is that the add_pinned_sites templatetag is undefined. Assuming it is in fact loaded in {% load tags %}, make sure that that templatetag library is available in all running environments, i.e. it literally exists on the server. If it is in fact there, make sure you completely reload your Nginx+FastCGI environment, or just reboot the server to be completely sure.
Is "tags" the actual name of the tag library that holds add_pinned_sites? Might be worth changing it to a clearer name-- just wondering if it's possible you're seeing import collisions between that and another tag library (like Django's built-in tags).