Django variables won't render in templates - python

Django version 2.1.3
Python version 3.7
Writing out sample code just to get an understanding of Django. Right now I'm on Templates and I'm having 0 luck when it come to rendering variables.
In views.py folder I've created a little dictionary and passed it through under the variable content
from django.shortcuts import render
# Create your views here.
posts = [
{
'Title': 'Challanger 1',
'Name': 'Sammy',
'Age': '33',
'Food': 'Seafood'
},
{
'Title': 'Challanger 2',
'Name': 'Sammy',
'Age': '33',
'Food': 'Seafood'
}
]
def home(request):
content = {
'posts': posts
}
return render(request, 'blog/home.html', content)
In my home.htmlfile, I've added a few 123 next to my {{variable}}to make sure the .html file is connecting to the view.py. When I py manage.py runserver, only the 123 is displayed but none of my {{variables}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{% for post in posts %}
<p>123 {{ post.name }}</p>
<h1>123 {{ post.title }}</h1>
<h1>123 {{ post.age }}</h1>
{% endfor %}
</body>
</html>
localhost:8000 produces:
123
123
123
123
123
123
When I open view-source from the browser:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>123 </p>
<h1>123 </h1>
<h1>123 </h1>
<p>123 </p>
<h1>123 </h1>
<h1>123 </h1>
</body>
</html>
Side note: When I'm in the home.html file I've noticed that only {{post.title}} and {{post.name}} auto-fill and {{post.age}} and {{post.food}} never auto-fill.
There are also times when I've deleted everything in the home.html file and none of the {{variables}} auto-fill when I rewrite out the code. Either way the ending result is still the same, variables wont load.

You're using post.name (all lowercase) in the template, but you defined the dictionaries in the python code to have Name (uppercase N) as the key.
Try using {{ post.Name }}.

Here is the problem, Dictionary Keys Are Case-Sensitive.
try below code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{% for post in posts %}
<p>123 {{ post.Name }}</p>
<h1>123 {{ post.Title }}</h1>
<h1>123 {{ post.Age }}</h1>
{% endfor %}
</body>
</html>

Related

How to store a portion of a result in python?

I have this code to get download links via libgen api.
#app.route("/api/default")
def titleSearch():
title = request.args.get('query')
s = LibgenSearch()
results = s.search_title(title)
item_to_download = results[0]
download_links = s.resolve_download_links(item_to_download)
return render_template(
"results.html", results=results, download_links=download_links, title=title,
)
results.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SUSBOOKS</title>
<link rel="stylesheet" href="{{ url_for('static', filename='bootstrap.min.css') }}" />
<link rel="stylesheet" href="{{ url_for('static', filename='mdb.dark.min.css') }}" />
<link rel="stylesheet" href="{{ url_for('static', filename='all.min.css') }}" />
<style>
body {
background-color:#000000 ;
}
</style>
</head>
<body>
<p>{{download_links}}</p>
</body>
download_links comes out as
{'GET': 'http://62.182.86.140/main/265000/ef7a3a2ecf51fa41f621f4e8b17df828/%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf', 'Cloudflare': 'https://cloudflare-ipfs.com/ipfs/bafykbzacebtnktd2zpvrcygswfcsr6wfft6jjyes6qi7f4mmottq3lebrgpsc?filename=%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf', 'IPFS.io': 'https://ipfs.io/ipfs/bafykbzacebtnktd2zpvrcygswfcsr6wfft6jjyes6qi7f4mmottq3lebrgpsc?filename=%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf'}{'GET': 'http://62.182.86.140/main/265000/ef7a3a2ecf51fa41f621f4e8b17df828/%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf', 'Cloudflare': 'https://cloudflare-ipfs.com/ipfs/bafykbzacebtnktd2zpvrcygswfcsr6wfft6jjyes6qi7f4mmottq3lebrgpsc?filename=%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf', 'IPFS.io': 'https://ipfs.io/ipfs/bafykbzacebtnktd2zpvrcygswfcsr6wfft6jjyes6qi7f4mmottq3lebrgpsc?filename=%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf'}
How would I go about making it output just the links hyperlinked so users can just click to download them?
In response, you have a dictionary which is key-value pairs.
reading key value of a dictionary:
my_dict = {
"link_1": "example_1.com",
"link_2": "example_2.com"
}
for key, value in my_dict.items():
print(key, value)
output
link_1, example_1.com
link_2, example_2.com
now you flask template you can do
{% for key, value in my_dict.items() %}
<p>{{ key }}, {{ value }}</p>
{% endfor %}
instead of
<p>{{ my_dict }}</p>

{% block content %} not working in django

I am learning about template inheritence in django
base.html:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>leshav</title>
</head>
<body>
<h1>My helpful timestamp site</h1>
{ % block content % }{ % endblock % }
<footer>
<hr>
<p>thanks</p>
</footer>
</body>
</html>
dateTime.html:-
{% extends "base.html" %}
{% block content %}
<p>today is {{ d|date:'d-m-y' }}.</p>
{% endblock %}
view for the function related to this page's url:-
def current_datetime_block(request):
now = datetime.datetime.now()
return render(request, "dateTime.html",{'d':now})
the problem is that it is not reading the content in the dateTime.html and is returning base.html file as it is
output:-
My helpful timestamp site
{ % block content % }{ % endblock % }
thanks
This is the output when I run the server
where the problem is coming? please help me
if anything else about the program is needed, you can ask me about it

Django csrf_token error, while I wrote it

Django csrf_token error, while I wrote it in my HTML file, but why does this error message poped up when I wrote that csrf_token?
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'f/ask/ask.css' %}">
<title>BareTalk</title>
</head>
<body>
{% block content %}
<div id="wrapper">
<form method="POST" action="">{% csrf_token %}
{{ form }}
<!-- TODO: input submit Reg() function javascript -->
<button name="final" id="final">Ask</button>
</div>
</form>
</div>
{% endblock %}
</body>
<script src="{% static 'f/ask/ask.js' %}"></script>
</html>
paste this code and try again ->
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'f/ask/ask.css' %}">
<title>BareTalk</title>
</head>
<body>
<div id="wrapper">
<form method="POST">
{% csrf_token %}
{{ form }}
<!-- TODO: input submit Reg() function javascript -->
<button name="final" id="final">Ask</button>
</form>
</div>
</body>
<script src="{% static 'f/ask/ask.js' %}"></script>
</html>

Flask template inheritance - not executing but no errors

I'm learning about template inheritance and I'm a little confused. When I navigate to index.html, I want the title to appear on the tab in my browser. And I want this functionality to be built into the base.html file, which I'm inheriting from, such that the function index on app.py need only be passed the argument title (and the html file name) to execute as described above.
For some reason, the code is not functioning as intended; the title is not present on the browser tab.
app.py:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html',title='index')
if __name__ == "__main__":
app.run(debug=True)
index.html:
<!DOCTYPE html>
{%extends "base.html"%}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
</head>
<body>
</body>
</html>
base.html:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
{%block head%}
{% if title %}
<title> {{title}} </title>
{%endif%}
{%endblock%}
</head>
<body>
{%block body%}{%endblock%}
</body>
</html>
Both index.html and base.html are found in the templates folder as is necessary for jinja to function. No errors are being triggered, the title is simply not being formatted as intended.
What am I doing wrong?
I figured it out, I needed to pull the title lines OUT of the block statement! Subtle but makes a big difference.
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
{%block head%}
{%endblock%}
{% if title %}
<title> {{title}} </title>
{%endif%}
</head>
<body>
{%block body%}{%endblock%}
</body>
</html>
Your base.html has two <title> tags. Which isn't valid html.
You'll want to remove the empty one and allow for the title to be passed as an argument in all cases.
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title> <!-- Remove this line here! -->
{%block head%}
{% if title %}
<title> {{title}} </title>
{%endif%}
{%endblock%}
</head>
<body>
{%block body%}{%endblock%}
</body>
</html>

Django - html not extending meta tags

I have a base.html file from where I extend other pages. I have this code in my base.html:
base.html
<html lang="es-es">
<head>
<title>
Home - {% block titulo %}{% endblock %}
</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/style/main.css" rel="stylesheet" />
</head>
All the templates extending from base.html have some spanish words and symbols and I have to put this meta tag again (so it doesn't throw me a unicode error), even when is on the base.html file:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
Aren't the templates supposed to inherit all the tags from the parent template? What is it that I'm doing wrong?
--EDIT 16 Mar 2016--
This is one example of a child. For the view I'm using: ListView.as_view in the url file, but not all the files extending from the base are using the same method although I do have to insert the meta tags in all the files for accents and other symbols to work.
{% extends "base.html" %}
{% load static %}
<!--The next 2 lines after this comment
are in every html files that extends from the base file
-->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% block titulo %}
Lideres
{% endblock%}
{% block content %}
<h2>Listado de Teléfonos</h2>
{% endblock %}

Categories

Resources