Django Alert box - javascript

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 %}

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 %}

Bootstrap alert message giving me unexpected token <

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 !!

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 %}

Open the xx.html file based on drop down menu from Navigation bar, code is written in Django-Python

The code is written in Django-Python. The project is created using the models in Django and shown in navigation as dropdown menu.
The drop-down menu is shown using the Django-HTML as shown in following way:
This code works well for dropdown menu. but I want to open the different project url based on click.
I am not sure exactly how to assign id and use javascript to code do onclick bsed html loading !!
I have tried some javascript code, but I am novice.. so If I put here.. it would be more confusing.
<div class="dropdown-menu" id="navbarDropdown">
{% if project_records %}
{% for p in project_records %}
{{ p.pName }}
{% endfor %}
{% endif %}
</div>
I expect that projectB.html will be loaded if click projectB in dropdown menu in navigation bar.
Add the href attribute in anchor tag appropriately. Assuming http://someurl/ is the prefix and the project name is the suffix of your project URLs, you may form the target URL in a variable and use it. Here is your modified code:
<div class="dropdown-menu" id="navbarDropdown">
{% if project_records %}
{% for p in project_records %}
{% with project_url="http://someurl"|add:p.pName %}
{{ p.pName }}
{% endfor %}
{% endif %}
</div>

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.

Categories

Resources