Django include tag - python

We are using django with python. I am facing a problem with include tag. I want to include a header in all modules of application.
In application templates directory contains all the html files with subfolders of modules. In any sub-module if I am creating header html and including this tag in base.html then it is working.
But if I place the header html in parent directory, its not working for any sub-modules.
I even tried {% include "../header.html" %} in html with django template, but no luck.
The project structure fo my application, in which root directory I have templates, static, handlers folder. Inside templates I have sub1 and sub2 folders. In sub1 I have base.html and in templates parent directory base.html, header.html, index.html. See below:
Root
----templates
----------Sub1
-----------------base.html
----------Sub2
----------base.html
----------header.html
----------index.html
----static
----handlers

Django templates include tag does not recognize relative paths. You need to give it the path under your templates directory, so try using the following instead:
{% include "Sub2/header.html" %}
Update:
Seems like your "header.html" are on the "Sub2" level and not inside it i.e. it's directly under templates directory So you should try:
{% include "header.html" %}

I am not sure if I have understood you. Just try {% include "header.html" %}, the search of django template should start from template folder.
The template folder is configured in Django config file "setting.py".
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
In addition, I have a project which can seprate Django template develop from the back-end using webpack and support jade, es6 and scss. The readme is in chinese :(, but you can run the project and check the source code.
https://github.com/njleonzhang/webpack-django-starter

Related

bootstrap_find_resource do not serve local files

I am using Flask-Bootstrap package for my Flask application, however when I am creating a template as described in docs:
{% extends "bootstrap/base.html %}
<!-- Rest of the template is here -->
page source displays this:
<!-- Bootstrap -->
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
I don't know why this resource is taken from cdnjs.cloudflare.com instead of local files in Flask-Bootstrap package.
cdnjs.cloudflare.com is referred in more places in page source code.
Can I somehow change this behavior in my template, to make it serve resources from local directory?
base.html template uses bootstrap_find_resource template filter, so I guess it have something to do with CDN settings, how can I change them?
Add this to your configuration :
app.config['BOOTSTRAP_SERVE_LOCAL'] = True
Check out Flask-Bootstrap link for more info.

How to view images in django html template

I keep getting 404 error even though the url to the image is accurate. Yes, I have looked at official django docs and a whole lot of stackoverflow posts. I couldn't figure it out from those. If anyone can decipher what the instructions are saying to do then I will be grateful because they seem to be missing information.
index.html:
#I have tried static and staticfiles
{% load static %}
#a lot of code later...
<img border="0" src="{% static "logo.png" %}" alt="TMS" width="125" height="80">
Directory structure:
projectname
->__init__.py, settings.py, ...
manage.py
db.sql3
app_name
-> admin.py, models.py, views.py, templates->index.html, ...
static
->logo.png
The only thing about static that I have in settings.py along with 'django.contrib.staticfiles':
STATIC_URL = '/static/'
My views.py file has index function which returns HttpResponse(t.render(c)) where t is the index.html template and c is Context. Could those be the issue?
The url that results is this: http://127.0.0.1:8000/static/logo.png
You are using a static directory which is outside of your app (it is in the project folder), so in settings.py you need also:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)

How to modify the admin template in django

i use the django's default admin for the admin site, and i create a app named "project"
Then in the project change form, i want to show and dynamic image, i plan to draw some charts using the google API, so i must put change the html form to add a in the html file as below:
So my question is that how can i modify the django's default templates for this page, i can not find the form in templates/ folder, is this page named "change_form"? "change_list"? or some other names. If so ,how do i only change the form for app "Project" since maybe i will create some other apps in the same level with "project"
You need a place for static files which is valid.
Then place an admin directory inside that static files directory.
Then add your addname as a directory like this:
Root
yourapp
static
admin
yourapp
file_to_overwrite
Find your Django files. Then go to:
contrib/admin/templates/admin
On OSX this would be:
/Library/Python/2.7/site-packages/django/contrib/admin/templates/admin
Copy the file you want to overwrite to yourapp directory like above.
EDIT:
If you want to change a single form for a single app:
For example:
Root
yourapp
static
admin
yourapp
modelname
change_form.html
Only the model "modelname" will be affected by this html-file.
A good way to do this as the docs suggests is go with #Rickard Zachrisson answer but instead of copying it use the "extends" block and import super.
this should be a better approach since you are not directly overriding the admin templates but instead inheriting them.
This is how the docs suggests to do it when inserting JS for example:
{% extends 'admin/change_form.html' %}
{% load static %}
{% block admin_change_form_document_ready %}
{{ block.super }}
<script type="text/javascript" src="{% static 'app/formset_handlers.js' %}"></script>
{% endblock %}

How to extend a base Flask Jinja template from a Blueprint template?

I am creating a fairly large application using Flask and Jinja. Flask recommends separating large applications into smaller units using Blueprints. If I have a base layout for my entire application/website, how can I extend this from templates within my blueprints?
You simply write name of the base template layout and Flask will find it if it exists in app's templates folder and then in blueprint's templates folder.
{% extends 'template_name.html' %}
If it exists inside a folder in templates folder then
{% extends 'folder_name/template_name.html' %}
If there are two templates with same name in app's templates folder and blueprint's template folder, then template in app's templates folder will get priority.
See this question for more info flask blueprint template folder
Flask automatically finds templates for you if they are placed at right positions.

assignment_tag TemplateSyntaxError

I try to use assignment_tag from django docs: https://docs.djangoproject.com/en/1.4/howto/custom-template-tags/#howto-custom-template-tags-simple-tags
Test project:
mysite/
manage.py
polls/
views.py
...
mysite/
...
templates/
polls/
detail.html
In polls/views.py:
from django.template import Library
register = Library()
#register.assignment_tag
def get_text():
return 'TEST TEXT'
Then I add code to templates/polls/detail.html
{% get_text as text %}
<p>The text is {{ text }}.</p>
But this is not work, I understand that get_text not visible, but I do not know how to do it right.
Your template tag should not go in the views.py file. You need to create a module in your app's templatetags directory. Have a look at the code layout docs for template tags.
Secondly, remember to load your tag in the template before you use it, with the {% load %} tag.
If you still have problems, update your question and include the full traceback -- 'template syntax error' isn't enough information for us to work out what's going on.

Categories

Resources