I have a template called reg_form.html in an application reg_manual. which looks similar to:
{% extends "registration/base.html" %}
{% block content %}
Some content
{% endblock %}
The base.html template has the following content:
<!DOCTYPE HTML>
<html>
<body>
<div class="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
Both templates are in the folder reg_manual/templates/registration/.
Now if run the following in a Django shell: (This error occurs also calling the URL from browser.)
from django.template.loader import render_to_string
render_to_string('registration/reg_form.html')
I get TemplateDoesNotExist: base.html error.
If I run render_to_string('registration/base.html'), the template is found and rendered. Also if I run the application on Windows with same settings.
This error occurs only on the production server (Ubuntu). Also changing the ownership of templates folder to www-data did not help.
Do somebody know a solution?
Related
Can anyone help me on how to add a logo to the index bar in Django-admin site?
You should probably override (by extend) the app_index.html admin template.
https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#templates-which-may-be-overridden-per-app-or-model
So you need to create a template/admin directory in your application and override the correct template by adding you own logo.
[Edit]
Oh sorry, you screenshot was too small and I just realized that you’re talking about the icon in the tabbar. That would be the favicon. For that, it’s simple, just add a favicon.ico into your static/ folder, and serve that directly from your http server (probably with a alias).
You can also serve the file directly using a simple view, but that doesn’t seems like a good idea except in development.
Your Question is answered here.
You have to override Django base.html template and put it under the admin directory.
https://stackoverflow.com/a/44428576/7122788
Override the Django base.html template and put it under the admin directory like my_app/templates/admin/base.html
Add {% block extrahead %} to the overriding template.
{% extends 'admin/base.html' %}
{% load staticfiles %}
{% block javascripts %}
{{ block.super }}
<script type="text/javascript" src="{% static 'app/js/action.js' %}"></script>
{% endblock %}
{% block extrahead %}
<link rel="shortcut icon" href="{% static 'favicon.ico' %}" />
{% endblock %}
{% block stylesheets %}
{{ block.super }}
{% endblock %}
I want to pass a model to template base.html.
I read about custom tags, and tried to execute this. It is not throwing any error, but is not working too.
My code:
base.html:
{% load staticfiles %}
{% load tags %}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<ul class="dropdown-menu" role="menu">
{% for league in get_my_leagues %}
<li> ddddd {{ league.league_name }}</li>
{% endfor %}
</ul>
{% block content %}
{% endblock %}
</body>
</html>
Now, tags.py:
from django.template import Library
from login.models import League
register = Library()
#register.inclusion_tag('base.html')
def get_my_leagues():
return League.objects.all()
register.tag('get_my_leagues', get_my_leagues)
When you use {% for x in y %}, this expects that y is a context variable in your template, not a template tag.
What an inclusion tag does is that it renders a template (the one you pass as argument to the inclusion_tag decorator), and inserts the result where the inclusion tag is used.
You probably want to register get_my_leagues as a simple tag instead (or an assignment tag, if you're using Django older than 1.9), and use it like this:
{% get_my_leagues as my_leagues %}
{% for league in my_leagues %}
...
{% endfor %}
guys.
I'm here just to tell that i found a solution for my problem. I'm using Context Processors to do this job.
Thank you all for answers!
I'm having trouble setting up Flask-bootstrap. From the official documentation, it seems like all I have to do is apply the Bootstrap constructor to my app, and the templates will be automatically created. So I tried this:
from flask import Flask
from flask.ext.bootstrap import Bootstrap
app = Flask(__name__)
Bootstrap(app)
#app.route('/')
def index():
return '<h1>Where is Bootstrap?</h1>'
if __name__ == '__main__':
app.run(debug=True)
When I run it, no new directories are created and I find no base templates anywhere. Am I missing a critical step? Is there some directory to be created first (I tried creating templates and static and setting the permissions to 777 but that didn't help)? Or perhaps the base templates are first to be generated from the command line?
The base templates are part of the Flask-Bootstrap package you installed. You extend the boostrap base template and override the blocks in it to create a page with a standard layout. You still need to write bootstrap related markup. This is explained right in the docs you linked to.
my_project/templates/my_page.html:
{% extends "bootstrap/base.html" %}
{% block title %}This is an example page{% endblock %}
{% block navbar %}
<div class="navbar navbar-fixed-top">
<!-- ... -->
</div>
{% endblock %}
{% block content %}
<h1>Hello, Bootstrap</h1>
{% endblock %}
I've created a custom tag that I want to use, but Django can't seem to find it. My templatetags directory is set up like this:
pygmentize.py
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from django import template
from pygments.formatters.other import NullFormatter
register = template.Library()
#register.tag(name='code')
def do_code(parser,token):
code = token.split_contents()[-1]
nodelist = parser.parse(('endcode',))
parser.delete_first_token()
return CodeNode(code,nodelist)
class CodeNode(template.Node):
def __init__(self,lang,code):
self.lang = lang
self.nodelist = code
def render(self,context):
code = self.nodelist.render(context)
lexer = get_lexer_by_name('python')
return highlight(code,lexer,NullFormatter())
I am trying to use this tag to render code in gameprofile.html.
gameprofile.html
(% load pygmentize %}
{% block content %}
<title>{% block title %} | {{ game.title }}{% endblock %}</title>
<div id="gamecodecontainer">
{% code %}
{{game.code}}
{% endcode %}
</div>
{% endblock content %}
When I navigate to gameprofile.html, I get an error:
Invalid block tag on line 23: 'code', expected 'endblock'. Did you forget to register or load this tag?
For Django 2.2 up to 3, you have to load staticfiles in html template first before use static keyword
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
For other versions use static
{% load static %}
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
Also you have to check that you defined STATIC_URL in setting.py
At last, make sure the static files exist in the defined folder
The error is in this line: (% load pygmentize %}, an invalid tag.
Change it to {% load pygmentize %}
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image">
in your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE.
did you try this
{% load games_tags %}
at the top instead of pygmentize?
{% load static %}
Please add this template tag on top of the HTML or base HTML file
Encountered this same issue but I just added {% load static %} from my extends template and it worked. So in your case if you're trying to load gameprofile.html from a base template you just need to add it like this:
{% extends 'some_base.html' %}
{% block content %}
{% load pygmentize %}
I had the same problem, here's how I solved it. Following the first section of this very excellent Django tutorial, I did the following:
Create a new Django app by executing: python manage.py startapp new_app
Edit the settings.py file, adding the following to the list of INSTALLED_APPS: 'new_app',
Add a new module to the new_app package named new_app_tags.
In a Django HTML template, add the following to the top of the file, but after {% extends 'base_template_name.html' %}: {% load new_app_tags %}
In the new_app_tags module file, create a custom template tag (see below).
In the same Django HTML template, from step 4 above, use your shiney new custom tag like so: {% multiply_by_two | "5.0" %}
Celebrate!
Example from step 5 above:
from django import template
register = template.Library()
#register.simple_tag
def multiply_by_two(value):
return float(value) * 2.0
The app that contains the custom tags must be in INSTALLED_APPS. So Are you sure that your directory is in INSTALLED_APPS ?
From the documentation:
The app that contains the custom tags must be in INSTALLED_APPS in order for the {% load %} tag to work. This is a security feature: It allows you to host Python code for many template libraries on a single host machine without enabling access to all of them for every Django installation.
In gameprofile.html please change the tag {% endblock content %} to {% endblock %} then it works otherwise django will not load the endblock and give error.
You need to change:
{% endblock content %}
to
{% endblock %}
I have two very simple templates like
index.html:
<html>
<head>
</head>
<body>
{% block content %}hello{% endblock %}
</body>
</html>
and details.html
{% extends "index.html" %}
{% block content %}{{ super() }} world{% endblock %}
but when i render a view with details.html i get this error
Could not parse the remainder: '()' from 'super()'
do i need some import somewere?
(templates are rendered properly until i use the super() function)
Django 1.7 and earlier do not support Jinja natively. Unless you've done something to use Jinja, your templates should be in Django template language, and you can't use Jinja.
Django 1.8 will have support for multiple template engines, and native support for Jinja2.
In Django template language, you can use {{ block.super }} to access the content of the block from the parent template.