TemplateDoesNotExist error due to a comment - html

I'm in the process of making a website with Django and have yet to create order_snippet.html. When I add {% include "order_snippet.html" %} I expect a TemplateDoesNotExist error, but I put this in the payment.html and commented it out like this: <!-- {% include "order_snippet.html" %} --> yet I still have a TemplateDoesNotExist error. I thought comments weren't read by html? How come this raises an error?

Django's template system knows nothing about HTML comments - it's a plain text templating system, you can use it to generate just any kind of text content. If you want to comment out part of the template code, use the template system features.

You have to comment the code with django comment template tags
https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#comment
{% comment %} {% include "order_snippet.html" %} {% endcomment %}
if you still need order_snippet.html make sure the directory is included in the settings.py

Related

Send variable along within include block

I'm working on a personal Django project where my plan is to make some sort of function in my site in form of a CSS Marquee (scrolling text).
I was able to make a marquee.html file with the code from here, and use it on several pages on my site using {% include "marquee.html" %} blocks, but the displayed string in the marquee is within the HTML file itself (marquee.html) between <p>-tags
Is there any way to send a variable/string along with a {%include "" %} block that replaces/adds to the <p> tags at the end of the marquee code?
(e.g. {% include "marquee.html" {{ stringToDisplay }} %} )
The current context is available for the included template. You can use the "with" option to send any additional context.
{% include "marquee.html" with message="Hello" %}
and in your marquee.html template
<div>{{ message }}</div>
The include documentation is here

Jekyll not rendering code in the right way

I have a blog that is based on jekyll now.But the issue I face is that the it is very difficult for me to write code here.I have already tried using <code> and ~~~ruby etc.None of them worked.This is the site for the blog.And this is the specific one I am looking at.This specifically is the repository where the blog is hosted.
No magic in Jekyll. Just Read The F.. Documentation (RTFM). See http://jekyllrb.com/docs/templates/#code-snippet-highlighting
{% highlight ruby %}
def foo
puts 'foo'
end
{% endhighlight %}
This just works.
Edit: be sure to leave a new empty line before the opening tag
<p>He had implemented ...<p>
{% highlight ruby %}

Django - Use variables from included template

I have the following setup:
base.html
...
{% block main-content %}
{% endblock main-content %}
...
admin.html
{% extends "base.html" %}
{% load staticfiles %}
{% block main-content %}
{% include users.html %}
{% endblock main-content %}
The file users.html uses tags like '{{ users }}' because it renders from a view that also returns several variables. Right now, if I call admin.html I can see the template of users.html (basic html, css) without the variables. I don't think the template is rendering from my views.py.
Is there anyway I can obtain the variables that the view is returning?
Note: base.html and admin.html are in the same django app, while users.html is in a different one.
Thank you!
This seems to be a common misapprehension.
Templates do not belong to views. The only relationship is that a view may (or may not) render a template: but a template itself may be rendered by one or many views, and has no actual knowledge of any of them. So when you "include" your template inside your admin template, there is no relationship to any other view that might also render it; if you need some variables in that view, you'll need to pass them there yourself.
Note that this sort of thing - that is, including a template along with some specific context variables - is usually best handled as an inclusion tag

Mezzanine Blog Page Title not Displayed

I am working on a client's Django/Mezzanine website that has got some strange issue that I just can't seem to figure out. On the blog page (template of blog_post_list.html) I cannot get the meta title of the page to display, meaning
{% block meta_title %}
{{ blog_page.title }}
{% endblock %}
produces no output in the resulting html. The same holds for the meta description, but I am not worried about it as much. The strange thing is that it seems to work just fine for individual blog entries, as well as all the other pages on the website, except the blog list.
Any ideas?
Nothing is displayed in Django template if you render not existing variable or variable value is None.
First test if {{ blog_page }} renders anything. If it doesn't check if blog_page is in your template context.
You can debug template's context by writing simple custom templatetag, e.g.:
templates/your_template.html:
{% load pdb from debug %}
{% block meta_title %}
{% pdb %}
{{ blog_page.title }}
{% endblock %}
templatetags/debug.py:
from django import template
register = template.Library()
#register.simple_tag(name='pdb', takes_context=True)
def pdb(context, *args, **kwargs):
import ipdb;
ipdb.set_trace()
Apparently, there was a bit of confusion involved: I thought blog_page.title was a standard variable in mezzanine, apparently it is not, it was a custom model created by the previous developer. Since I have basically only the templates and a dump of the DB, it does not appear to be possible to restore the original model for blog_page class, so I simply solved it by supplying a meta title manually in the blog_post_list template.

Render static html page inside my base django template

I'm developing a web portal using
- Django 1.2
- Apache
- Mod WSGI
I've several HTML files which are being served by apache.
I want to render those static HTML pages under my base template in order to keep my header / footer and dynamic menus intact.
One way I could thought its using iframes. Another way is to do read HTML files and return string while rendering but in that case I'm loosing advantage of apache, so I want to know if there would be any better way of doing it, is there any existing solution provided by django stuff ?
I'm not sure if this is exactly what you're asking for, but you can insert an html file (or even another template) in a template with the ssi and include tags, depending on your needs:
{% ssi '/path/to/file.html' %}
{% include 'relative/path/to/template.html' %}
yes, it's the include tag
Loads a template and renders it with the current context. This is a way of "including" other templates within a template.
it's as simple as
{% include "templates/static_template_1.html" %}
or, if you create a variable in the view side:
{% include template_name_variable %}
it shares the context with the base template (the one including them)
Edit:
Perhaps you ment to load html-files outside the template-system. Then my way will not suffice.
An option is to extend your base template.
Your base template should not be aware of the sub templates as that would be logically wrong.
Example:
base_template.html:
<html>
<div id='header'></div>
{% block content %}
This text can be left out else it it will shown when nothing is loaded here
{% endblock %}
sub_template.html:
{% extends "base_template.html" %}
{% block content %}
<h1>This is my subpage</h1>
{% endblock %}
You can read more here:
https://docs.djangoproject.com/en/1.2/topics/templates/