Hy, i am very new to web dev and jinja2. i encountered a problem with jinja, when i render template and want to output user name to web page who loged in...well i cant get one. Here is my index.html file with jinja expression in tag:
{% extends "base.html" %}
{% block content %}
{% if user %}
<h1>Welcome to Flask authentication site, {{ user.name }}!</h1>
{% else %}
<h2>Hello, log in or sign up</h2>
{% endif %}
{% endblock content %}
Related
I am trying to freeze my Flask blog app with Frozen Flask but the problem is I can't get the pagination to work correctly after the freeze().
I'm using the app factory pattern.
Here's my main.routes.py:
#bp.route('/home')
#bp.route('/index')
#bp.route('/')
def index(form=None, methods=['GET', 'POST']):
latest_posts = load_latest_posts(10)
with db_session(autocommit=False):
page = 1
posts = load_all_posts().paginate(page, 10, False)
next_url = url_for('main.index', page=posts.next_num) \
if posts.has_next else None
prev_url = url_for('main.index', page=posts.prev_num) \
if posts.has_prev else None
if current_user.is_anonymous:
return render_template('main/index.html', title='Home', posts = posts,
prev_url=prev_url, next_url=next_url, latest_posts=latest_posts)
load_all_posts() does what is says, returning Post.query.order_by(Post.pub_date.desc())
load_latest_posts(n) is basically the same but fetches the latest (n) posts.
As you see, I'm passing the pagination object to posts which I use in my main/index.html template to render the pagination items:
{% extends 'base.html' %}
{% block posts_preview %}
{% for post in posts.items %}
{% include 'posts/_post.html' %}
{% endfor %}
{% endblock posts_preview %}
{% block footer %}
<ul class="pagination">
{% if prev_url %}
<li>«</li>
{% endif %}
{% for page_num in posts.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=3) %}
{% if page_num %}
{% if posts.page == page_num %}
<li><a class="active" href="{{url_for('main.index', page=page_num) }}">{{ page_num }}</a></li>
{% else %}
<li>{{ page_num }}</li>
{% endif %}
{% else %}
...
{% endif %}
{% endfor %}
{% if next_url %}
<li>»</li>
{% endif %}
</ul>
{% endblock footer %}
_post.html is nothing fancy, just another template that includes post structure.
If I run this in Flask, it works without a problem. When generating static site with Frozen Flask, page numbers are there but clicking on them wouldn't redirect me anywhere. I see the URL being changed from http://127.0.0.1:5000/ to http://127.0.0.1:5000/?page=2 but the new content doesn't load, only refreshing the current page.
What might be the issue here ? How can I load pages and pagination correctly?
According to the Frozen Flask documentation on how the filenames are generated:
Query strings are removed from URLs to build filenames. For example,
/lorem/?page=ipsum is saved to lorem/index.html. URLs that are only
different by their query strings are considered the same, and they
should return the same response. Otherwise, the behavior is undefined.
This means that, unfortunately, http://127.0.0.1:5000/ and http://127.0.0.1:5000/?page=2 will refer to exactly the same page. To get pagination to work, you'd need to make sure that the page number was part of the URL before the query string - something like http://127.0.0.1:5000/page2/.
I am doing a django book store project which has a successful sign up function. I followed WS Vincent's tutorial to connect Github signup (https://learndjango.com/tutorials/django-allauth-tutorial) and I have come across an error when integrating it.
TemplateSyntaxError at /
Invalid block tag on line 16: 'provider_login_url', expected 'endif'. Did you forget to register or load this tag?
Below is the code for the major areas of my project.
home.html
{% extends '_base.html' %}
{% load static %}
{% block title %}Home{% endblock title %}
{% block content %}
<h1>Homepage</h1>
<img class="bookcover" src="{% static 'images/djangoforprofessionals.jpg' %}">
{% if user.is_authenticated %}
<p>Hi {{ user.email }}!</p>
<p>Log Out</p>
{% else %}
<p>You are not logged in</p>
<p>Log In |
Sign Up
Sign Up</p>
{% endif %}
{% endblock content %}
I am not familiar with HTML but I take the two sign up lines are not meant to be put one after the other.
According to the documentation, it seems like you forget to load {% load socialaccount %}.
Reference
I'm trying to reset the password_reset_email.html template in my django app by adding this file in templates/registration/password_reset_email.html:
{% load i18n %}{% autoescape off %}
{% blocktranslate %}You're receiving this email because you requested a password reset for your user account.{% endblocktranslate %}
{% translate "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% translate 'Your username, in case you’ve forgotten:' %} {{ user.get_username }}
{% translate "Thanks for using our app!" %}
{% blocktranslate %}The team{% endblocktranslate %}
{% endautoescape %}
However it raises a TemplateSyntaxError:
'blocktranslate', expected 'endautoescape'. Did you forget to register
or load this tag?
What am I doing wrong?
I integrated an app into the cms using the documentation. All ist set but the CMS doesnt show the app content. It seems base.html will not show the content of my app.
Could it be my app-used template ?
list.html
{% extends CMS_TEMPLATE %}
{% load render_table from django_tables2 %}
{% load cms_tags sekizai_tags staticfiles %}
{% block main %}
{% addtoblock "css" %}<link rel="stylesheet" href="{% static "django_tables2/themes/paleblue/css/screen.css"%}">{% endaddtoblock %}
{% render_table doctor_htmltable %}
{% endblock main %}
I figured it out. although I still do not find it mentioned anywhere in the documentation.
In my base.html a block is defined:
{% block content %}{% endblock content %}
and so everything the app should display must also be include into a block with the same name
{% block content %}
....(see lines in question)
{% endblock content %}
I have a Django (1.6) application that inherits a base template. I would like to include one of my (currently working) flatpages into the application landing page, something that the Django docs say is possible.
Here is my template:
{% extends "path/to/base.html" %}
{% load flatpages %}
{% get_flatpages as fp %}
{% block content %}
<h3>Flatpage inclusion</h3>
<p>Number of flatpages: {{ fp|length }}
<ul>
{% for page in fp %}
<li>{{ page.title }}</li>
{% endfor %}
</ul>
{% endblock content %}
This does not list any of the flatpages. However, if I remove the {% extends %} signal, so my code looks like this:
{% load flatpages %}
{% get_flatpages as fp %}
<h3>Flatpage inclusion</h3>
<p>Number of flatpages: {{ fp|length }}
<ul>
{% for page in fp %}
<li>{{ page.title }}</li>
{% endfor %}
</ul>
Everything works. I see the number of flatpages in my fp object (9) and my unordered list shows all the flatpage urls and titles.
This seems to me to be a bug in either how flatpages work, or how Django does template inheritance.
The base template (/path/to/base.html) doesn't have anything complex in it.
Django categorically says that this is possible:
When you load a custom tag or filter library, the tags/filters are only made available
to the current template – not any parent or child templates along the template-inheritance path.
For example, if a template foo.html has {% load humanize %}, a child template (e.g., one that
has {% extends "foo.html" %}) will not have access to the humanize template tags and filters.
The child template is responsible for its own {% load humanize %}.
This is a feature for the sake of maintainability and sanity.
Has anyone else noticed this bug? Is it an exception for just the built-in flatpages app?
EDIT 1:
Daniels answer is correct. The example code from the Django docs doesn't show including flatpage content within a {% block %}, so I didn't expect that it needed to be done:
{% load flatpages %}
{% get_flatpages as flatpages %}
<ul>
{% for page in flatpages %}
<li>{{ page.title }}</li>
{% endfor %}
</ul>
My fault I guess. Live and learn.
The problem is that your get_flatpages tag is outside any blocks from the parent template. That means it simply won't be called.
Move it into the content block and it should work.
Just to reiterate the correct solution- get_flatpages needs to be placed inside the block where it's going to be referenced. So this will work:
{% extends "index.html" %}
{% load flatpages %}
{% block footer %}
{% get_flatpages as flatpages %}
{% for page in flatpages %}
...
{% endfor %}
And this will not work:
{% extends "index.html" %}
{% load flatpages %}
{% get_flatpages as flatpages %}
{% block footer %}
{% for page in flatpages %}
...
{% endfor %}
And yes, Django documentation isn't very clear on that.