What is the correct syntax for the href on an html page with Jinja2 code, that allows for navigation between two pages? The first html template has a list of names, while the other has person details.
Here is the code I have for the listOfNames.html page that displays the list of names.
<ul>
{% for rownum, row in listNames.iterrows() %}
<li>{{ row.firstName }} {{ row.lastName }}</li>
{% endfor %}
</ul>
Here is the server.py code that gets/puts (correct use of term?) the names on the listOfNames.html.
#app.route('/listNames/<bo>/')
def listNames(bo):
listNames = getListNames(bo)
return render_template('listOfNames.html', listNames=listNames)
This is code for the personInformation.html.
<main role="main" class="col-sm-9 ml-sm-auto col-md-10 pt-3">
<h1>{{ person.firstName }}
{{ person.lastName }}
</h1>
<h2>Office:
{{ person.bo }}
</h2>
<h2>Courses Completed
</h2>
<ul>
{% for rownum, row in personCompleted.iterrows() %}
<li><a href="/courses/{{row.courseId}}">
{{row.courseTitle}}
</a></li>
{% endfor %}
</ul>
</main>
And here is the server.py code.
#app.route('/people/<person_id>')
def person(person_id):
person = getPersonOrganization(person_id)
personCompleted = getPersonCompleted(person_id)
return render_template('personInformation.html', person=person, personCompleted=personCompleted)
The best way to do this is by using the url_for() function from Flask. Here is your new listOfPeople.html template with the link:
<ul>
{% for rownum, row in listNames.iterrows() %}
<li>{{ row.firstName }} {{ row.lastName }}</li>
{% endfor %}
</ul>
It's best to not hardcode your URLs in the templates, because if you ever need to reorganize them, then you would need to update URLs all over the place. With url_for() Flask takes care of generating the URLs for you using the information you provided in the app.route decorators.
Related
I want to create a list of entries in which each entry is linked to its page like this: wiki/entry-title. I'm using a for loop to add <li>s to HTML. here's the code:
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
urlpattern:
path('wiki/<str:title>', views.entry, name='entry')
what should I type in href to link the <li> to wiki/entry?
You can set your value in url as
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
Its better to use {% url %} [Django-doc] template tags as
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
NOTE : change value with your value accordingly. For e.g. {{entry.value}} or {{entry.title}} or {{entry.id}}
There is a clear example in the django docs
<ul>
{% for yearvar in year_list %}
<li>{{ yearvar }} Archive</li>
{% endfor %}
</ul>
In django, the template parser always handles django code first, then html/javascript. So you would insert a django variable into an anchor tag the same way you'd insert it anywhere in the template and the parser will replace it before it tries to render the html. If it's a django url, you can use the {% %} format as referenced in the previous answer, and if it's a url that's perhaps stored on the object, you can just use {{ }} (like {{ entry.wiki_url }}). You can also use text for some of the url and a variable for part. So if you have a wiki site that has a base url of, for instance, https://mywiki.com you'd write the href like:
<ul>
{% for entry in entries %}
<li>{{ entry.title }}</li>
{% endfor %}
</ul>
I found out that the only solution that was working for me was this format
{{ x }}
x: this is variable that we want to implement inside href attribute
'url_path_name' : this is the name of the url we gave in urls.py , see :
path("wiki/<str:name>" , views.entry, name="url_path_name")
So this defined url should look like this:
wiki/ ?
href="{% url 'url_path_name' x %}"
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.
I want to print value by id in database,And don't know which keywords to find in Google.
in my views.py, I send transen = TransEn.objects.all() to template
and this will print all datas from database:
{% for words in transen %}
{{words.words|safe }}
{% endfor %}
But I want to print by the value of the id Like:
(Because they are words in English for translating website)
I don't know how to write this in template, please guide me, Thank you very much.
<div><span> TransEn.objects.filter(id='2') </span></div>
<div> TransEn.objects.filter(id='3') </div>
UPDATE:
I have found a method:
I can use if tag, but are there another ideas??
<div>
{% for words in transen %}
{% if words.id == 2 %}
{{ words.words|safe }}
{% endif %}
{% endfor %}
</div>
<div>
{% for words in transen %}
{% if words.id == 3 %}
{{ words.words|safe }}
{% endif %}
{% endfor %}
</div>
If you want to access each item in the QuerySet individually, by index, you should cast it to a list first. You should change your views.py to:
transen = list(TransEn.objects.all())
And then in your template you can access them by index like so:
<div><span> {{ transen.1.words }} </span></div>
<div> {{ transen.2.words }} </div>
A warning from the Django docuemtnation about casting a QuerySet to a list:
Be warned, though, that this could have a large memory overhead, because Django will load each element of the list into memory. In contrast, iterating over a QuerySet will take advantage of your database to load data and instantiate objects only as you need them.
I want to create custom breadcrumbs template for my site developed with django-cms.
It should display levels (number) of items in addition to item title. I've read this article and it seems that I should use {{ node.level }} like this, for example:
{% for ance in ancestors %}
<li>
{% if not forloop.last %}
**{{ ance.level }}** - {{ ance.get_menu_title }} <span class="separator">ยป</span>
{% else %}
<span class="active">**{{ ance.level }}** -{{ ance.get_menu_title }}</span>
{% endif %}
</li>
{% endfor %}
But this is didn't working. I exepected to see "0 - Main ..." but see only " - Main ..."
And I am also curious about get_menu_title because I didn't find that in documentation, but I am find {{ node.title }} and it seems working same as get_menu_title
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 }}