Building urls with django template system - python

So I need to build an url querying multiple objects. I tried to use the template system from django to build it with no success. I was wondering if there is any way of doing this in the template or if I should just write a view function.
{% url 'service-ticket/new' as request %}
{% with request|add:"?" as request %} {% endwith %}
{% for entry in object.items.all %}
{% with request|add:"spareparts="|add_string:entry.item.id|add:"&" as request %}
{% endwith %}
{% endfor %}
<a class="btn-add float-right" href={{ request }} role="button">
THIS SHOW HAVE ALL
</a>

Related

Django include template, overwrite 'self' variable?

I've been tasked to fix a template structure in a Django app. I am not very familiar with Django as I deal mostly with front end projects built in React.
The Django app is using Wagtail as a backend CMS. I am looping through it's pages, and including a template block and trying to pass variables with the with statement on the include. I can successfully pass variables to the template block, but I need to overwrite the 'self' variable as the template block is looking for self.something.
Here is what my template looks like.
{% load static wagtailimages_tags wagtailcore_tags %}
<div class="c-card {% if self.variant %}c-card--{{ self.variant }}{% endif %}">
{% if self.image %}
{% image self.image original class="c-card__image" loading="lazy" %}
{% endif %}
{% if self.title %}
<h3 class="c-card__title">{{ self.title }}</h3>
{% endif %}
{% if self.text %}
<p class="c-card__content">{{ self.text }}</p>
{% endif %}
{% if self.button.title and self.button.url %}
{% include './button_block.html' with button=self.button %}
{% endif %}
</div>
and then there is the loop and include snippet...
{% for chapter_menu in page.chapters %}
<div class="medium-6 large-4 columns">
{{ chapter_menu.value.title }}
{% include './blocks/card_block.html' with self=chapter_menu.value %}
</div>
{% endfor %}
Doing this self=chapter_menu.value results in a TypeError, which is expected as I am defining self twice. push() got multiple values for argument 'self'
How can I go about doing this? Any docs I can read besides the basic django templating docs? https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#with
add the only attribute to include.... Did not find this in any documentation...
{% include './blocks/card_block.html' with self=chapter_menu.value only %}

How do i use the less than or greater than signs inside html in python django

Hi am struggling using the greater than sign inside the html template. am using python 3.7.2 and django 1.8.6
I cant execute this code here gives an error
{% extends "base.html" %}
{% block Content %}
<h1>Tags</h1>
{% for instance in object_list %}
<li><a href='{{ instance.get_absolute_url }}'>{{ instance.title }}</a> ({{ instance.products.count }} product{% if instance.products.count > 1 %}s{% endif %})</li>
{% empty %}
<li>No Tags yet.</li>
{% endfor %}
</ul>
{% endblock %}
So the > 1 is the problem here
help out

how to use name space instead of hard coded url in django templales for pagination

i am newbie in django,i have django templates where i have added the following code for pagination.here you can see that i have apply hard coded url for pagination.but i don't want to use hard coded url ,i want to use namespace instead of the hard coded url.how can i do this.
Template:
<span class="page-links">
{% if page_obj.has_previous %}
{% if query_string %}
previous
{% else %}
previous
{% endif %}
{% endif %}
</span>
my urls:
url(r'^(?P<chain_pk>[0-9]+)/full/combination/$',
CombinationSearchList.as_view(), name='dash_combination_search_list'),
Update
my django version is 1.6
<span class="page-links">
{% if page_obj.has_previous %}
{% if query_string %}
previous
{% else %}
previous
{% endif %}
{% endif %}
</span>
Please note that I have used point.id on the url to provide the value for the point.id in your hardcoded url. This can be any other variable, like object.pk or object.id or any other context variable. If this is not clear post again with the view code of the page and I can help.

How to create a header that can be added into files flask

I am wondering how to create a jinja2 template that would allow my to put my header into a variable where I can extend the base file and then call upon the header in my child file.
Currently my code for the parent is:
{% block head %}
<div class="wrapper col2">
<div id="topbar">
<div id="topnav">
<ul>
<ul>
<li>Home</li>
<li>Sign in/up</li>
<li>Admin console
<ul>
<li>Console</li>
<li>Staff Management</li>
<li>ALERTS</li>
<li>Sign up Shooters</li>
</ul>
</li>
<li >Contact Us</li>
<li class="last">logout</li>
</ul>
</div>
<br class="clear" />
</div>
<hr>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }} </li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</div>
{% endblock %}
My child template is:
{% extends "Header.html" %}
{% block head %}
{% endblock %}
<h1>CREATE SHOOT</h1>
<form action="" method="post" name="form">
{{ form.hidden_tag() }}
<p>
<h2>Name of shoot:</h2>
{{ form.nameofshoot(size=40) }}<br>
<h2>Number of days in shoot:</h2>
{{ form.day}}
<p><input type="submit" value="Create Shoot"></p>
</form>
Am I doing something wrong or is there another approach I can take?
What you need to do is remove from your child template the lines {% block head %}{% endblock %}
Because in your child template you call {% extends "Header.html" %}, all of the content declared in Header.html (no matter what blocks it is in) will automatically be included. That's the job of extends.
By adding the {% block head %}{% endblock %} in your child template, what you have effectively said is "take everything from Header.html, but override everything in the 'head' block with what I specify in my child template". Then, because there is nothing in the block defined in your child template, you are replacing the content defined in your Header.html file with nothing.
You want to use the {% block blockname %} notation to specify what you want to change in the child template, not what you want to keep the same.
This page on inheritence is the relevant section of the Jinja documentation, and provides a nice example.

Misaka template tags for Django - text wrapped in double quotes

I'd like to replace the standard Markdown implementation in a Django blog I'm building with Github-flavoured Markdown. I'd like to use Misaka, and I've thrown together my own custom template tags. Unfortunately, something has gone awry.
Here is my template tags file, which is in blog/templatetags/gfm.py. The __init__.py file is present in the same folder:
from django import template
from django.template.defaultfilters import stringfilter
import misaka as m
register = template.Library()
#register.filter(is_safe=True)
#stringfilter
def gfm(value):
rendered_text = m.html(value,
extensions=m.EXT_FENCED_CODE,
render_flags=m.HTML_ESCAPE)
return rendered_text
And here is one of my templates:
{% extends 'layout/base.html' %}
{% block header %}
{% endblock %}
{% block content %}
{% load gfm %}
{% if object_list %}
{% for post in object_list %}
<div class="post">
<div class="page-header">
<h1>{{ post.title }}</h1>
</div>
{{ post.text|gfm }}
<p>Posted {{ post.pub_date }}</p>
<p>
{% for category in post.categories.all %}
<a class="badge badge-info" href="/category/{{ category.slug }}/">{{ category.title }}</a>
{% endfor %}
</p>
</div>
{% endfor %}
<br />
<ul class="pager">
{% if page_obj.has_previous %}
<li class="previous">Previous Page</li>
{% endif %}
{% if page_obj.has_next %}
<li class="next">Next Page</li>
{% endif %}
</ul>
{% else %}
<div class="post">
<p>No posts matched</p>
</div>
{% endif %}
{% endblock %}
The outputted text is being returned wrapped in double quotes, which breaks the whole thing. Otherwise, the markup generated seems correct.
Where have I gone wrong here? I know it's not the data in the database as if I use pdb to get the values of value and rendered_text inside the function, they are rendered correctly. For example, here is the plain text version of one post, as printed by pdb:
u'A Python application:\r\n\r\n print "Hello world"'
And here is the version rendered in Markdown using Misaka:
u'<p>A Python application:</p>\n\n<pre><code>print "Hello world"\n</code></pre>\n'
I'm fairly experienced with Django, but I'm new to custom template tags.
Use autoescape tag.
{% autoescape off %}{{ post.text|gfm }}{% endautoescape %}
Alternatively you can use safe filter.
{{ post.text|gfm|safe }}

Categories

Resources