I am new to Django and I am practicing template inheritance. I am currently having trouble inheriting templates on the 3rd level. The base level is a template that my whole site uses (ex: navbars). The second level is the content of my site. However this content is a bit lengthy so I took a portion(contactform.html) of it and created its own HTML file for that portion.
I am able to get my home.html into my index.html like so
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head lang="en">
<link href="{% static "css/boothie.css" %}" rel="stylesheet" type="text/css">
<script src="{% static "js/boothie.js" %}"></script>
<script src="{% static "js/jquery.easing.1.3.js" %}"></script>
<title>Boothie</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Within my home.html I want to include my contactform.html. This is what I have so far.
{% extends "index/index.html" %}
{% load staticfiles %}
{% block content %}
...
...
...stuff...
<!-- contact -->
{% block contactform %}{% endblock %}
{% endblock %}
My contactform.html:
{% extends "home/home.html" %}
{% load staticfiles %}
{% block contactform %}
<section id="contact">
<!-- HTML! -->
</section>
{% endblock %}
This is what is currently in my home/views.py:
from django.shortcuts import render
from django.views import generic
class HomeView(generic.TemplateView):
template_name = "home/home.html"
my home/urls.py:
from django.conf.urls import patterns, url
from home.views import HomeView
urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name="home"),
)
TEMPLATE_DIRS:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'home'),
)
here is a picture of my project structure:
Instead of inheriting, just include the contactform template in the home template. In home/home.html put:
<section id="contact">
{% include 'home/contactform.html' %}
</section>
You dont need to extend, instead include
Example, keep the contents of the contact_form.html with just the required html content (without the extends, and the block tag, etc..), and then include the html snippet. Now, django would do the magic for you - The included snippet would have all the context variables too.
{% extends ".." %}
{% load staticfiles %}
{% block content %}
...
...
...stuff...
{% include /path/to/contactform.html %}
{% endblock %}
Related
I am using Django, I am trying to display the image but I am getting the error.
Invalid block tag on line 35: 'static', expected 'endblock'. Did you
forget to register or load this tag?
If I added my image directly on index.html page then the image is displaying but when I am using extends and block to display then I am getting the error.
setting.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)
home.html
{% extends 'demo1/index.html' %}
{% block content %}
<img src="{% static 'images/web/landing-page.png' %}" alt="Landing Page">
{% endblock %}
index.html
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title%}Home{% endblock %}</title>
<link rel="stylesheet" href="{% static 'css/style.css'%}" type="text/css">
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
You missed the open { in your home-page
{% extends 'demo1/index.html' %}
{% load static %}
{% block content %}
<img src="{% static 'images/web/landing-page.png' %}" alt="Landing Page">
{% endblock %}
NOTE
Django documentation prefers now {% load static %}.
{% load staticfiles %} works but I think it is deprecated.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#static
Update:
From the Django docs:
The include tag should be considered as an implementation of "render
this subtemplate and include the HTML", not as "parse this subtemplate
and include its contents as if it were part of the parent". This means
that there is no shared state between included templates -- each
include is a completely independent rendering process.
Therefore please also load the static file in your home-page too
Do just {% load static %} on top of your home.html template.
You are missing a brace in your home.html
{% extends 'demo1/index.html' %}
{% block content %}
<img src="{% static 'images/web/landing-page.png' %}" alt="Landing Page">
% endblock %}
should be
{% extends 'demo1/index.html' %}
{% block content %}
<img src="{% static 'images/web/landing-page.png' %}" alt="Landing Page">
{% endblock %}
The static files from third-party apps are not being updated in my Django site when it is served up in a browser with runserver.
Here is my file structure (with many more static files):
- mysite
- mysite
- settings.py
- wsgi.py
. . .
- myapp
- templates
- base.html
- myapp.html
- models.py
- forms.py
. . .
- static
- MyApp
- mystyle.css
- autocomplete-light
- select2.css
base.html:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>
{% block title %}Portal{% endblock title %}
</title>
{% block stylesheets %}
<link rel="stylesheet" type="text/css" href="{% static 'myapp/mystyle.css' %}?{% now "U" %}"/>
{% endblock stylesheets %}
</head>
<body>
{% block content %}
{% endblock content %}
</body>
<footer>
{% block footer %}
{% endblock footer %}
</footer>
</html>
myapp.html:
{% extends "myapp/base.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block title %}My App{% endblock title %}
{% block stylesheets %}
{{ block.super }}
{% endblock stylesheets %}
{% block content %}
{% crispy formset helper %}
{% endblock content %}
{% block footer %}
<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}?{% now "U" %}"></script>
{{ formset.media }}
{% endblock %}
settings.py (relevant piece):
STATIC_ROOT = 'C:/Users/username/mysite/static/'
STATIC_URL = '/static/'
I used python manage.py collectstatic to collect the static files for my app and the third party app (django-autocomplete-light) into the STATIC_ROOT. They exist in the STATIC_ROOT. I then served up my site with runserver. The static files are loaded with the site and their paths match back to the STATIC_ROOT. When I edit the static files (i.e. select2.css for the autocomplete app), the changes show up in STATIC_ROOT. However, they do not show up in the browser.
I have tried clearing my cache, serving the site in various browsers, killing/restarting runserver, killing/restarting my text editor, and rerunning collectstatic. None of these attempts have worked - the static files simply will not update when the site is loaded.
How do I get the static files to update in the browser? I think I might be either missing a setting in settings.py, or it may be an issue with {{ formset.media }}. I am at a loss, though.
Remember add STATICFILES_DIRS, Django need the url to the staticfiles:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
'/var/www/static/', # If you have more staticfiles this will be the order
)
and also take a look to this What is the difference between {% load staticfiles %} and {% load static %}
I'm currently trying to create a homepage for Django, but when I test it, it just shows me a blank page with nothing on it. This is what I have so far. First is my URL page from mysite:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('firstapp.urls')),
]
and my url page from firstapp:
from django.conf.urls import url
from firstapp.views import HomePage
urlpatterns = [
url(r'^$', HomePage.as_view(), name='home'),
]
this is my views page:
from django.shortcuts import render
from django.views.generic import TemplateView
class HomePage(TemplateView):
template_name = 'home/home.html'
def home(request):
return render(request, self.template_name)
this is the base.html which extends to home.html
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
<link rel='stylesheet' href='{% static "css/base.css" %}'/>
</head>
<html>
<p>{{ variable }}</p>
<script src= '{% static "js/base.js" %}'></script>
</body>
</html>
And finally home.html itself.
{% extends 'base.html' %}
{% block body %}
<div class="container">
<p>Testing Home</p>
</div>
{% endblock %}
Now if it works, I should be seeing "Testing Home"...but...I see nothing. I just get an empty white page. Any idea why this is happening?
Your base.html doesn't have a {% block body %} defined anywhere in it. You need blocks to be defined in the template you're extending, otherwise you'll have nothing to override inside the extended template.
base.html
<html>
<body>
<p>{{ variable }}</p>
{% block body %}{% endblock %}
<script src= '{% static "js/base.js" %}'></script>
</body>
</html>
Note: I also fixed your opening <body> tag which you incorrectly had as <html>, leaving you with two <html> opening tags in your template.
In the static folder of my DjangoServer is located a template of the default webpage. It's decorated with some template blocks.
If I load this template file, the path to the template is shown in the browser, it looks like, that the code is not loaded.
If I store the template in an app/template folder and I extend this file. It works very well. I use the tutorial of Django but it still not working.
Settings.py
django.contrib.staticfiles is added to INSTALLED_APPS
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
projectRootFolder/static/html/basePage.html
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
<title>{% block title %}My amazing site{% endblock %}</title>
{% endblock %}
</head>
<body>
{% block body %}
<header>
{% block header %}
<header> -- HEADER BANNER --</header>
{% block menu %}<nav></nav>{% endblock %}
{% endblock %}
</header>
<section>
{% block section %} SECTION {% endblock %}
</section>
{% block footer %}
<footer> -- FOOTER --</footer>
{% endblock %}
{% endblock %}
</body>
<script type="text/javascript" src="{% static 'angularjs/SOME_ANGULAR_FILES_LOADED.js' %}"></script>
</html>
app/template/app/index.html from an app
{% load static from staticfiles %}
{% static "html/basePage.html" %}
{% block menu %}<nav>App A</nav>{% endblock %}
{% block section %} Lorem Ipsum{% endblock %}
app/views.py
from django.template import loader
from django.shortcuts import render
from django.http import HttpResponse
# PAGE CALLS
def index(request):
template = loader.get_template('mainControll/index.html')
context = {}
return HttpResponse(template.render(context, request))
Output
What is the mistake i've made? How can I load this template correctly?
Warning, this answer assumes your goal for asking the question is to get things to work, instead of helping you troubleshoot a probable permission problem just so that you can run into another problem. That is, I'm assuming your end goal is not a html document inside a non-html document.
What can you do to get your output to work:
It looks like you want to include a template into another template, you can do that with {% extends "basePage.html" %}. Your template does then need to be at a location where templates are found, not where static pages are found.
E.g. If your app is called 'myapp' then under myapp/templates/ is a one possible place, assuming the TEMPLATES setting has APPDIRS = True
This would mean changing index.html to be
{% extends "basePage.html" %}
{% block menu %}<nav>App A</nav>{% endblock %}
{% block section %} Lorem Ipsum{% endblock %}
See https://tutorial.djangogirls.org/en/template_extending/ for example of this and https://docs.djangoproject.com/en/1.11/topics/templates/#configuration for configuring things to that your basePage.html can be found
I am getting started with Django, and I'm trying to make a modular template, but I don't know how. Now, I have the following files:
1- base.html (which provides basic layout for all the website):
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>My site</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/bootsrap.min.css' %}" />
</head>
<body>
<h1>Test title</h1>
{% block content %}
{% endblock %}
</body>
</html>
2- index.html (main db read)
{% extends 'base.html' %}
{% block content %}
{% if latest_smartphones_list %}
<ul>
{% for s in latest_smartphones_list %}
<li>{{ s.brand }} {{ s.name }}</li>
{% endfor %}
</ul>
{% else %}
<p>No smarphones available.</p>
{% endif %}
{% endblock %}
Finally, i wanted to add a third file, called menu.html which would contain the site menu. I wanted to add it in the base.html file. I've been thinking about doing it in the following way, but i doesn't work:
{% load 'menu.html' %}
Thanks so much for your help!
Instead of using {% load 'menu.html' %} you have to use {% include 'menu.html' %}
Docs:
include
load
The correct way is using the include templatetag
{% include 'menu.html' %}
which includes a template and renders it with the current context.
NB: whenever you are in trouble, django docs is the best place to go to! Always keep this in mind