Hello and thank you in advance...
In Django I have a child template that updates a base.html template. I keep getting an error:
<ExtendsNode: extends "base.html"> must be the first tag in the template.
When the code in the child template looks like this:
{% extends "simple/base.html" %}
{% block picklist %}
<title></title>
<h1>Simple Index</h1> #...
I dont get why I am getting this error, when the EXTENDS tag is clearly the first tag.
Both the base and the child template live in the same directory, in my template path /simple.
Thank you,
dp
I figured this out...
I had another {% extends %} tag on the templet which was commented out using HTML <!-- --> comments. The comments were ignored and the extends tag was read and generated the 503 error.
Thank you,
dp
Related
For example passing in a snippet
{% include 'icon-top', classes:'back-to-top__icon' %}
I can pass the class back-to-top__icon into icon-top snippet
<svg class="icon {{ classes }}" somesvg stuff here ></svg>
Doing the same with a section doesn't work - is there any way to do this in liquid?
Sections doesn't accept anything outside of the section file. You can look the section like a closed platform nothing comes inside or outside of the section.
The means that variables created outside/inside the section are not accessible inside/outside of it.
That said you can hack it slightly to achieve what you want.
For example:
The section file:
test.section.liquid
The section file code:
<div class="{{dummy_class}}"></div>
Then you call the section this way:
<div style="display: none;">
{% section 'test.section' %}
</div>
{% capture section_capture %}
{% section 'test.section' %}
{% endcapture %}
{{ section_capture | replace: '{{dummy_class}}', 'back-to-top__icon' }}
Clarification
You might be asking why are we calling the section two times?
When we call the section in a {% capture %} tag it's not shown in the admin panel that's why are showing it in a hidden div only to show it in the admin and we don't do anything else with it.
After that we capture the section in a variable section_capture, this will return the content of section and we can replace anything we want in there.
That's why we added this {{dummy_class}} dummy variable. It's wrapped in liquid but you can treat it as text and not liquid, so we can write it like so #dummy_class# as well.
After that we just target that string and replace it {{ section_capture | replace: '{{dummy_class}}', 'back-to-top__icon' }}
I'm doing a navbar in django and in the links I' need an output like this: "/category/1/", "/category/2/", "/category/3/", what is the method to do this in urls templete tags?
I already made: {% url 'my_app:category' category.id=1 %} not working
well, finally I' did this:
<ul>
{% for category in categories%}
<li>{{category}}</li>
</ul>
{% empty%}
<li>Empty!</li>
{% endfor%
that worked for me.
I have an application that creates several "bokeh" plots. Those plots all belong to the same document, so that i can use linked panning / zooming. The plots are served by a "bokeh" server. All plots appear in one website but there is some html content between the plots. All of that is part of a django app. Using bokeh-0.12.1
#view.py
plots = []
plot1 = figure()
plot1.line([1,2,3],[5,4,2])
plot2 = figure()
plot3.line([1,2,3],[5,4,2])
script_tags = []
bokeh_document = curdoc()
session = push_session(bokeh_document)
script.tags.append(autoload_server(model=plot1, session_id=session.id))
script.tags.append(autoload_server(model=plot2, session_id=session.id))
Then in the template would look as follows:
template.html
<h1>These are the embedded server plots</h1>
{% for script in script_tags %}
<pre>{{ subgroup_plot.script }}</pre>
<h1>Here comes a plot</h1>
some Text
<div>
{{script | safe }}
</div>
{% endfor %}
But then the page gets rendered imporperly... The plots appear over each other and over the page content. The documentation does not mention how to embedd it in html. So how does it have to be done so that the css works correctly?
It looks like there are missing some CSS classes, namely bk-root and plotdiv. Try the following:
<h1>These are the embedded server plots</h1>
{% for script in script_tags %}
<pre>{{ subgroup_plot.script }}</pre>
<h1>Here comes a plot</h1>
some Text
<div class=“bk-root">
<div class=“plotdiv">
{{script | safe }}
</div>
</div>
{% endfor %}
Also refer to the CSS file to see the correct order/nesting of classes.
autoload_server had an issue in 0.12.1 that prevented the correct enclosing <div class="bk-root"> from appearing. This issue was fixed in 0.12.2, you can either upgrade, or put them in by hand as the other issue demonstrates.
I have a UpgradeView with the same possibility for saving like in the django admin. I can save, save and continue editing or save and create a new object.
Each leading to a different view:
DetailView, UpdateView and the CreateView.
After saving I want to give a message out, on every view or template its leading to.
For example "Successfully saved" or "Object could not be saved."
When writing custom template tags it's getting really hard for me, because after I created the tag, I don't know how, where and when to pass the message to the other views.
This is the UpdateView where i come from.
class TopicEditView(UpdateView):
fields = ['title','description',]
model = Topic
...
def get_success_url(self):
if self.request.POST.get('save'):
return reverse('topic_detail', kwargs={'pk':self.object.pk})
elif self.request.POST.get('save_and_continue'):
return reverse('topic_edit', kwargs={'pk':self.object.pk})
elif self.request.POST.get('save_and_create_new'):
return reverse('topic_create')
else:
return reverse('fallback_success_url')
My custom template tag is still empty, because the only examples i saw are pretty hard to understand for me.
#register.inclusion_tag('msg.html', takes_context=True)
def get_msg(context):
return None
Inside 'msg.html' i only have the string saying "Successfully saved", this did lead to nothing and i forgot why i did that.
And this is in my template (nothing):
{% load msg_handler %}
{% get_msg %}
How and where can I pass the message to these views using the session?
Try the Django messages framework instead:
http://docs.djangoproject.com/en/1.8/ref/contrib/messages
I got this faster done than I ever thought.
This is my custom template tag:
#register.simple_tag(takes_context=True)
def get_msg(context):
try:
return context.request.session.pop('msg')
except KeyError:
return ''
And this is my view, passing the message:
class TopicEditView(UpdateView):
...
def get_success_url(self):
self.request.session['msg']='Successfully saved!'
...
Nothing have changed in my template.
If there is a more elegant/useful way, i would appreciate it.
EDIT :
Thanks to Lorenzo Peña's comment, i tried using the messages framework which was really easy to use!
First i went to my views.py again and imported messages
from django.contrib import messages
and changed this line
self.request.session['msg']='Successfully saved!'
To this:
messages.add_message(self.request, messages.SUCCESS, 'Successfully saved!')
Then i made a new template called msg_loader.html containing this:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
And changed in my other templates this
{% load msg_handler %}
{% get_msg %}
to this
{% include "msg_loader.html" %}
I have a liquid template where I need to render a partial inside that.
Please note #current_page.page_layout.content will load the content from the DB.
My liquid layout file is as follows:
#layouts/public.html.erb
<%= Liquid::Template.parse(#current_page.page_layout.content).
render('page_content' => yield, 'page_title' => yield(:title)) %>
and following is my code, which includes the partial as well
{{page_content}}
{% include 'this_is_the_partial_name' %}
and I'm getting this error
Liquid error: This liquid context does not allow includes.
I tried searching the web and found this solution, but still I'm not sure what to enter for this code:
Liquid::Template.file_system =
Liquid::LocalFileSystem.new(template_path)
liquid = Liquid::Template.parse(template)
Little late to the party.. but this is how you should use it:
In an initializer (like /config/initializers/liquid.rb) add:
template_path = Rails.root.join('app/views/snippets')
Liquid::Template.file_system = Liquid::LocalFileSystem.new(template_path)
Add your partial file, eg. app/views/snippets/_partial_name.liquid.
Now in your liquid template use:
{% include 'partial_name' %}