Bootstrap alert message giving me unexpected token < - javascript

I am working on a django project and in my html file, I have this code snippet.
{% block content %}
{% ifequal error False %}
<script>
<div class="alert alert-success">
<strong>Login Successful</strong>
</div>
window.location.href="{% url 'home' %}";
</script>
{% endifequal %}
{% ifequal error True %}
<script>
<div class="alert alert-success">
<strong>Wrong credentials! Try again</strong>
</div>
window.location.href="{% url 'login' %}"; </script>
{% endifequal %}
{% endblock content %}
In the console, I am getting this message:
Uncaught SyntaxError: Unexpected token '<'
On line 500, it is saying. Basically, it points at the line where there is a div within the first script tags. The bootstrap alert is not showing and there is no redirection. If I put a simple alert,everything works! Any idea what is wrong?
UPDATE:
I already solved it. Did another way
Thanks !!

Related

HTML if else condition for printing items in list

I'm making a website with flask, and I pass in a list. My .html file has this code:
{$ for file in list %}
<div>
<img ... > </img>
</div>
{% endfor %}
Now if list.length == 0 I want it to just do something like
<h2> Error </h2>
instead. How do I make an if statement to check if the list is empty, and if it is then I print error on the website, if it is not, I print the images in the list. I tried to put:
<script>
if(list.length==0){
<h2>Error</h2>
}
else{
"the for loop stuff"
}
</script>
but this does not work.
THanks
In Flask with Jinja2 as a template engine, the if-else syntax requires you to write the following statement
{% if mylist.length == 0 %}
<div>error</div>
{% else %}
<!-- loop -->
{% endif %}
Note: Since list is a Python keyword i suggest you to use another name for your list in order to avoid possible conflicts.
In this pythonprogramming page about flask 7, you will find some example code and a video. The example shows an if-else statement:
{% for post in posts %}
<h3> {{ post.title }}</h3>
<p>{{ post.body }}</p>
{% if author %}
<span style="font-size:0.5em;color:yellowgreen">{{ post.author }} </span>
{% endif %}
{% if date %}
<span style="font-size:0.5em;color:yellowgreen">{{ post.date }}</span>
{% endif %}
{% endfor %}
I decided to post my answer to share this learning resource because I have noted that the example does not use any <script> tag (not sure if it helps).
The comparison operator is almost the same everywhere as you can see in both the Python and Jinja references.
Therefore, the equivalent for your code would be:
{% if list.length == 0 %}
...
{% endif %}

access opened html div in javascript

i call ajax request in A.html and when response is ok I want to show message in B.html.
(I want to show message in a div with id='mes_div' that contains in B.html )
how can I access B.html and
how should I access this div?
I use django as server side.
You should do it like this:
Django view:
default case: -> template A.html
if condition == Load_A_Done -> template B.html
On A.html's AJAX, after loading success you call the Django View, with a form parameter like load=OK
On Django view, check if load parameter is presenting, if no, use the A.html as template, else use the B.html as template
Before using B.html, use messages
from django.contrib import messages
def viewAB(request):
if request.GET.get("load") == "OK":
messages.info(request, 'A loaded successfully')
return render(request, 'B.html')
else:
return render(request, 'A.html')
on the B.html
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
{% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}Important: {% endif %}
{{ message }}
</li>
{% endfor %}
</ul>
{% endif %}

Django Alert box

I want to show a JavaScript alert box when there is an error during login (wrong password or username), and reload the page. How can I do this from the Django template?
This is along the lines of what I had in mind:
[% if messages %}
<div class="javascript:alert">
{{ somemessage }}
</div>
{% endif %}
Put your alert call inside a script tag like this:
[% if messages %}
<script>
alert({{ somemessage }})
</script>
{% endif %}

Django: making {% block "div" %} conditional with a conditional {% extends %}

I would like to share a template between AJAX and regualr HTTP calls, the only difference is that one template needs to extend base.html html, while the other dose not.
I can use
{% extends request.is_ajax|yesno:"app/base_ajax.html,app/base.html" %}
To dynamically decide when to extend, but I also need to include {% block 'some_div' %}{% endbock %} tags to tell the renderer where to put my content. The ajax call needs those tags to be left out because jQuery tells it where to put the content with $('somediv').html(response).
Any idea on how to dynamically include those block tags when its not an ajax call?
I've been referencing this question to figure it out:
Any way to make {% extends '...' %} conditional? - Django
Attempt to make it work through an {% if %}:
{% extends request.is_ajax|yesno:",stamped/home.html" %}
{% if request.is_ajax == False%}
{% block results %}
{% endif %}
{% load stamped_custom_tags %}
...
Content
...
{% if request.is_ajax == False%}
{% endblock %}
{% endif %}
but this fails when parser runs into the {% endif %}
Wouldn't this, or some variant, do?
{% if request.is_ajax %}
{# ajax! #}
{% else %}
{% block 'some_div' %}{% endbock %}
{% endif %}
My Solution:
{% extends x|yesno:"stamped/blank.html,stamped/home.html" %}
Where blank.html contains:
{% block results %}{% endblock %}
<!-- to allow for corrected shared rendering
with ajax posts and normal django rendering -->
and home.html is my standard app page with a results block to extend.

Twig: comments in {% javascripts %}

I have a situation like this in my twig file:
{% javascripts
'js/functions.js'
'js/plugins.js'
'js/editor.js'
'js/calendar.js'
%} [...]
And I would love to comment some scripts out using
{% javascripts
{# 'js/functions.js' #}
'js/plugins.js'
'js/editor.js'
'js/calendar.js'
%} [...]
But this isnt allowed, I get an Unexpected character "#" error.
Is there a common practice how to handle this?
That is unsupported twig syntax, obviously. So it's up to you how to hide it. I would do something like this:
{# 'js/functions.js'#}
{% javascripts
'js/plugins.js'
'js/editor.js'
'js/calendar.js'
%} [...]

Categories

Resources