I have a navbar that shows active when it is in the current section. I have internationalised the web so now the url includes the language code. How can I add the language code in the if condition below?
{% if '/{LANGUAGE_CODE}/accounts/dashboard/' == request.path %} active {% endif %}
webpage url: http://127.0.0.1:8000/en/accounts/dashboard
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<aside class="col-md-3">
<!-- SIDEBAR -->
<ul class="list-group">
<a class="list-group-item {% if '/{LANGUAGE_CODE}/accounts/dashboard/' == request.path %} active {% endif %}" href="{% url 'dashboard' %}"> Dashboard </a>
[continues...]
I tried {{LANGUAGE_CODE}} and some pasting. Any ideas how to get the if condition working?
You can use add filter to concatenate your strings and with tag to cache this complex variable. The result would look like this:
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<aside class="col-md-3">
<!-- SIDEBAR -->
<ul class="list-group">
{% with "/"|add:LANGUAGE_CODE|add:"/accounts/dashboard/" as dashboard_url %}
<a class="list-group-item {% if dashboard_url == request.path %} active {% endif %}" href="{% url 'dashboard' %}"> Dashboard </a>
{% endwith %}
[continues...]
Related
I have a list view in a modal window, once the user chooses the option of the select element, the pagination changes the url queryset (?p=<page_number>), although, in a normal list view there is no issue,as it changes the url, in a modal, it changes the entire page's location (URL address), which of course causes the lost of changes in the main page (document form), I just need the modal list to be changed by the number of pagination-page.
I have searched and read almost all documentations pages related to pagination of a list view using Django, however I was unable to find the solution.
here is the modal pagination template code (see the comments that indicates not working parts):
{% load wagtailadmin_tags %}{# Changed by HH #}
{% if linkurl %}{% url linkurl as url_to_use %}{% endif %}
{% if items.has_previous or items.has_next %}
<div class='pagination center' aria-label="Pagination">
<!-- this is working -->
{% if items.has_previous %}<div class="l_arrow previous">Previous</div>{% endif %}
<p>{% with pr=items.paginator.page_range page_num=items.number total_pages=items.paginator.num_pages %}
Page {{ page_num }} of {{ total_pages }}
<!-- this is NOT working -->
<select onchange="gotopage(this)" name="pg_selector" title="pg_selector" id="pg_selector" disabled>
{% for i in pr %}
<option value="{{ url_to_use }}{% querystring p=i %}" data-page="{{ i }}" {% if i == page_num %} selected {% endif %}>{{i}}</option>
{% endfor %}
</select>
{% endwith %}
</p>
<!-- this is working -->
{% if items.has_next %}<div class="r_arrow next">Next</div>{% endif %}
</div>
{% endif %}
Edit: Temporary Workaround by using text input (please do NOT Consider this as an answer)
{% load wagtailadmin_tags %}{# Changed by HH #}
{% if linkurl %}{% url linkurl as url_to_use %}{% endif %}
<script>
function goto_pg(){
let pg_selector_el = document.getElementById("pg_selector");
pg_selector_el.href = `{{ url_to_use }}?p=${document.getElementById("pg").value}`;
pg_selector_el.click();
}
</script>
{% if items.has_previous or items.has_next %}
<div class='pagination center' aria-label="Pagination">
<!-- this is working -->
{% if items.has_previous %}<div class="l_arrow previous">Previous</div>{% endif %}
<p>
{% with pr=items.paginator.page_range page_num=items.number total_pages=items.paginator.num_pages %}
Page <input id="pg" type="text" value="{{page_num}}" onkeyup="goto_pg()" > of {{ total_pages }}
{% endwith %}
</p>
<!-- this is working -->
{% if items.has_next %}<div class="r_arrow next">Next</div>{% endif %}
</div>
{% endif %}
I'm trying to build a authentication based view! My html code is:
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<link href="{% static "css/base.css" %}" rel="stylesheet">
</head>
<body>
<div id="header">
<span class="logo">Bookmarks</span>
{% if request.user.is_authenticated %}
<ul class="menu">
<li {% if section == "dashboard" %}class="selected"{% endif %}>My dashboard</li>
<li {% if section == "images" %}class="selected"{% endif %}>Images</li>
<li {% if section == "people" %}class="selected"{% endif %}>People</li>
</ul>
{% endif %}
<span class="user">
{% if request.user.is_authenticated %}
Hello {{ request.user.first_name }}, Logout
{% else %}
Log-in
{% endif %}
</span>
</div>
</body>
</html>
Here the line:
request.user.first_name
supposed to show the logged in username according to Django By Example book.
also when I'm changing the password in firefox it shows this massage:
But in chrome it works fine in changing password, although the name is not showing in both browser!
My Login page html code is :
{% extends "base.html" %}
{% block title %}Log-in{% endblock %}
{% block content %}
<h1>Log-in</h1>
{% if form.errors %}
<p>
Your username and password didn't match.
Please try again.
</p>
{% else %}
<p>Please, use the following form to log-in:</p>
{% endif %}
<div class="login-form">
<form action="{% url 'login' %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}" />
<p><input type="submit" value="Log-in"></p>
</form>
</div>
{% endblock %}
I'm stuck really bad :(
This is silly! The problem was I didn't set the user's firstname!
But the second problem is still happening!
i had same issue, but after some research, i found this solution
so try to edit the variable from {{ request.user.first_name }} TO {{ request.user.username}}, so is username not firstname.
I have a base template that I'm trying to extend. With in that template I have a navigation that needs a variable passed.
What I was doing was trying to 'extend' it by using an include like so:
{% include "base.html" with active_nav='atInventory' %}
but this doesn't render the blocks of the base with the extended content in the right order.
The navigation is set up in the base like so:
<nav class="col-xs-12 paddingVertical-sm">
<ul class="removePadding removeMargin txtXxs">
<li class="{% if active_nav == 'atContact' %} activeNav {% else %} inactiveNav{% endif %}">
<a href="{% url 'contact' %}">CONTACT<a>
</li>
<li class="{% if active_nav == 'atAbout' %} activeNav {% else %} inactiveNav{% endif %}">
ABOUT
</li>
<li class="{% if active_nav == 'atProjects' %} activeNav {% else %} inactiveNav{% endif %}">
PROJECTS & CLINICS
</li>
<li class="{% if active_nav == 'atServices' %} activeNav {% else %} inactiveNav{% endif %}">
SERVICS
</li>
<li class="{% if active_nav == 'atInventory' %} activeNav {% else %} inactiveNav{% endif %}">
INVENTORY
</li>
<li class="{% if active_nav == 'atHome' %} activeNav {% else %} inactiveNav{% endif %}">
HOME
</li>
<br class="clear-fix">
</ul>
<img src="{% static 'images/assets/klossviolins_logo.png' %}" />
<br class="clear-fix">
</nav>
Is there a better way to do this? Or does this just require a slight tweak? Thanks in advance for any help on this.
nI wouldn't do this - extend it the way it was intended. Include is for including reusable components. Here you just want every template to extend your 'base.html' with {%extends 'base.html'%}
Just a pointer as well - I'd do the active nav with a tiny script instead of tons of if statements. So add an ID to each li element (#nav-home for index in my example), and on your home page have something like:
<script>
$(document).ready(function() {
$( "#nav-home").addClass("activeNav");
});
</script>
How do I display a group of 6 thumbnails in my template to create a filmstrip. The list i am iterating through a for loop has around 30 items i want to break it into chunks of 6 and show it as a filmstrip slider. I tried using the range function for the for loop but that doesnt work
<div class="carousel-inner">
<div class="item active">
<ul class="thumbnails">
{% for show in object_list %}
<li class="span2">
<div class="thumbnail">
<a href="{{ show.get_absolute_url }}" data-title="{{ show.name }}" >
{% if show.images.exists %}
{% with show.images.all.0.image as show_image %}
<img src="{% thumbnail show_image 160x160 crop %}" alt="{{show.name}}" class="thumbnail">
{% endwith %}
{% else %}
<img src="{% static 'img/default-image.gif' %}" alt="{{show.name}}" class="thumbnail">
{% endif %}
</a>
</div>
</li>
{%endfor%}
</ul>
</div>
If you want 1 carousel per 6 images then you could do it like this
Step 1) Create a new .html file in your templates folder called film-slider.html.
{% for reel in reels %}
<div class="carousel-inner">
<div class="item active">
<ul class="thumbnails">
{% for show in reel %}
<li class="span2">
<div class="thumbnail">
<a href="{{ show.get_absolute_url }}" data-title="{{ show.name }}" >
{% if show.images.exists %}
{% with show.images.all.0.image as show_image %}
<img src="{% thumbnail show_image 160x160 crop %}" alt="{{ show.name }}" class="thumbnail">
{% endwith %}
{% else %}
<img src="{% static 'img/default-image.gif' %}" alt="{{ show.name }}" class="thumbnail">
{% endif %}
</a>
</div>
</li>
{% endfor %}
</ul>
</div>
</div>
{% endfor %}
Step 2) In your templatetags/tags.py (create it if you haven't)
from django import template
register = template.Library()
def filmslider(reel):
reels = []
for i in range(0, len(reel), 6):
reels.append(reel[i:i+6])
return {'reels':reels}
register.inclusion_tag('film-slider.html')(filmslider)
This will make an inclusion tag that's available in your templates once you've loaded it via {% load tags %}.
This will make this work for you like this {% filmslider object_list %} which you will replace all the above html code that you posted with.
I haven't tested this but it should work, if in the future you want to have more functionality to this tag you can simply add arguments to the tag definition, I'll give an example.
def filmslider(reel, slides):
#do the code.
which will lead you to {% filmslider object_list 9 %} and voila, now you can extend your film reel from 6 slides to 9.
Hope this helps a bit!
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).