I am working on a Python/ Django project, having not really used either much before. I am currently getting a TemplateSyntaxError when clicking a link on one of the pages on my website.
The URL that this link takes you to (the URL for the broken page) is: costing/id/payment-report/overview & the exception value says:
Invalid block tag on line 87: 'date_to_display', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag?
The template (HTML file) itself for this page, doesn't actually have this variable anywhere in it. The location of the template in the folder structure is: costing/templates/costing/reports_post_deposit.html, however, there is another template file at costing/pdf2_base.html, which does contain this variable within the structure:
<body>
...
{% block content_payment_schedule %}
{% if not webview %}
<div>
<table>
...
<tr>
...
<td> {% date_to_display %} </td>
</tr>
</table>
</div>
{% endif %}
...
{% endblock %}
...
</body>
So I can see that the variable it's complaining about is used within this template. The page at costing/id/payment-report/overview is used to generate a PDF file from information stored in the database, and information provided by the user. The structure of this PDF file is defined in the costing/pdf2_base.html file- and date_to_display is a a variable whose value I wanted to add to the PDF generated...
What do I need to do to register or load this tag, as the error message says I need to do?
when displaying a variable in django templates you use
{{variable}}
so to fix your issue change
{% date_to_display %}
into
{{date_to_display}}
You can check how to display variables in django template in the following link;
https://docs.djangoproject.com/en/1.10/topics/templates/#variables
Related
I am trying to make a blogging website. I know django provides argument templates like
{% include images.html with value=sense %}
The above code directly works in HTML and hence everything works. The images are stored in a backend database and connected to everystory by some logic. The user can use the names of the images and call whenever they need to use it
When I try the above code directly in the backend it doesn't work because I think once something is rendered then it doesn't rerender by django HTML
I wish to paste some form of links in the django story backend. such that when it renders in HTML automatically the page should show pics in the appropriate place. If anyone has any idea how to do this kindly let me know.
So when loading stories in the database the user can put some form of links for images in the database and while rendering all images come in a certain format as specified in the block in the blog.So there can be any number of images and the count is not longer fixed as shown in the pics below where I am trying to render a image called sense from the backend which doesn't work.. whereas it directly works in the frontend.
<p>{{object.story_title}}</p>
<p>{{MEDIA_ROOT}}</p>
<p>{{object.story}}</p>
{% include "blogdescription/image.html" with value=sense %}
Thank you for your time.
with regards
Let me start saying that doing exactly what you want is not possible because Jinja will compile and render {{object.story}} and not its content (the include). It does not seem possible to use nested Jinja syntax to load any resources, includes, extends, urls, etc.
Which explains why when you place the include in the template it works but does not inside your model field.
What seems possible is to load an HTML image with a explicit URL to the resource, lets say, the content inside your text field is:
<div style="display: flex; justify-content: center; align-items: center;">
<img src="/static/myimage.jpg" alt="Object Image">
</div>
Template.html (source):
{% block content %}
{{obj.title}}
<br>
{{obj.body|safe}}
{% endblock %}
Alternatively, it is possible to generate a HTML file to render dynamically based on Object.field. Note that this solution is a heavy load on the server, for every request will generate a dynamic file to be rendered.
Obj field value:
{% extends 'base.html' %}
{% block content %}
{{obj.title}}
<hr>
{% include 'includes/image.html' %}
{% endblock %}
views.py:
def story(request, id):
obj = Story.objects.get(id=id)
f = open(f'templates/generated/dynamic_template.html', 'w+')
f.write(obj.body)
f.seek(0)
return render(request, 'generated/dynamic_file.html', {'obj': obj})
I have a form I'm working with in Django.
I have a built in error message I'm trying to get to render on the form.
My first step is to get the error message to render on the form and then I will go into the function and tweak when it shows up.
My problem emerges when it comes to doing it in python.
Normally, my preferred way would be to JQuery for the footer and use JavaScript to append/prepend the HTML. Then set it to show/hide based on conditionals.
However, for this I am wanting to do it in Python to make it easier for the people working w/ me.
Here's an example of the error message HTML I would like to use for appending to something else in JS.
error_field.append('<em for="name" class="form-error-message text-danger">');
Here is an example of the Django Code Block I would like to add it within
{% block form-footer %}
{{ block.super }}
{% endblock %}
What is the easiest way to accomplish this within Python/Django? To be clear, I can figure out the conditional stuff myself. Just the rendering of the specific HTML/CSS error class I have already created. I should be able to do the conditional/function part of this myself.
I can just show you an example, this is a part of my project
views.py
try:
user=User.objects.get(username=username)
except:
messages.info(request,'username does not exist')
return redirect('login')
return render(request,'users/login-register.html')
html
{% if messages %}
{% for i in messages %}
<div class="alert alert--{{i.tags}}">
<p class="alert__message">{{i}}</p>
<button class="alert__close">x</button>
</div>
{% endfor %}
{% endif %}
You can use this anywhere in your html page. This is a common page, and everything in here is extended. And of course this is an example similar to your problem. Check it out if you want
Please, I would like to know how I would run a python function that takes two parameters from within an "html" - this function is inside a class ("python" file).
Example:
utils.py
...
def get_calc(val_old, val_inc):
return val_old + val_inc
index.html
{% load static %}
...
{{ get_calc "1" "3" as datawowdelay }} <= this is correct
<div class="container">
<h2>{{ datawowdelay }}</h2> <= this is correct
</div>
...
My thanks for the help
thank you very much for your help and feedback.
I tried to use the "Custom template" previously, but it didn't work either!
The function is not exposed so that it can be used or accessed from within an "html" page, see this:
utils.py
from django import template
register = template.Library ()
#register.simple_tag
def get_calc (val_old, val_inc):
return val_old + val_inc
When referencing the function in the "html" file, the error below occurs:
Invalid block tag online 45: 'get_calc'. Did you forget to register or load this tag?
So, I tried to add a load.
index.html
...
<body>
{% block content %}
{% load utils %}
{% endblock %}
And also
...
<body>
{% block content %}
{% load static utils %}
{% endblock %}
Returns the error:
'utils' is not a registered tag library
At the end of it all, if I remove what I'm trying to do, the page is successfully rendered in the browser. And on this same page I have a "for" that scans the contents of a list and assembles and builds several items using "Django Phyton" syntax.
Just the question of trying to call a function and passing parameters that doesn't work at all.
Thank you very much for any help.
I have installed django-page-cms successfully i think. Like other cms, it is also for creating new pages. But I already have html pages in my project. How to integrate with that?
They want me to put place holder in html page, like:
{% load pages_tags %}
but I think this will bring the content from the already created page in admin
Can anyone tell me how to integrate with my existing pages?
First you need to create page in admin console. Then add the placeholder in your template
like what tutorial saying
{% get_page "news" as news_page %}
{% for new in news_page.get_children %}
<li>
{{ new.publication_date }}
{% show_content new body %}
{% endfor %}
I am building a Django website and my side bar can have different elements for different users. So my main sidebar template has a div for every plugin to be included and the specific HTML for every one of these plugins is included in their own template file.
example:
<div id="plugins">
<div id="plugin1">
{% include 'plugin1.html' %}
</div>
<div id="plugin2">
{% include 'plugin2.html' %}
</div>
</div>
Now I want to build this list dynamically how could I do it? As the template is only parsed once so I could not send it a '{% include 'plugin1.html'}' string in the context
Any ideas?
You can use a variable inside the include tag:
{% include my_user_html %}
You can generate a variable in the view as above containing your template or you can use a template tag to generate the template path for you based on another variable, i.e. a phase. Register the following tag, customize it to your needs:
#register.filter
def get_template_phase(template_string, phase):
template_string = template_string.replace('<', '{').replace('>', '}')
return template_string.format(phase=phase)
Place the above in your templatetags and register it.
Usage:
{% include 'includes/home__<phase>.html'|get_template_phase:'nomination' %}