Author Picture

Kim Majali


Show Loading Message After Submitting a Form

Author Avatar wrote on 06/06/2022

If we have a view that it takes time to load, we want to show the user a message to wait, it takes some time

What I want to do
1. Create div to be loaded 2. Create JS to load that div after submission 3. Create Partial template 4. Include the partial template inside our base template and use it when needed 5. Add code to the form to fire the JS after submission

JS code in partial

We have created the div and the JS and added them inside the template partials/_loading-modal.html Create partials/_loading-modal.html and place the code below

# Include it base.html

{% include 'partials/_loading-modal.html' %}

# Use when submitting a form

add the class show-loading-after-submit to any form you want to show the loader

# Use when clicking on a URL to load a view

add the class show-loading-after-click to any url (a element) you want to show the loader

Note: If you want to test with these CSS classes, you can get the open-source CSS file at agilecss.com Resource: https://stackoverflow.com/questions/8317219/django-show-loading-message-during-long-processing Read more

Django Display Objects Data from Database

Author Avatar wrote on 06/06/2022

Boolean prints in a django template?


  {{ bool_var|yesno:"Agree,Disagree" }}
 
Or

 {% if var == True %} Yes {% else %} No {% endif %}
</code Read more

Iterating Through Two Lists in Django Templates

Author Avatar wrote on 06/06/2022

Views.py

zipped_segments = zip(source_segments, target_segments)
for s_segment, t_segment in zipped_segments:
    print('s_segment', s_segment)
    print('t_segment', t_segment)
    print('---------------------')
Read more

Authentication in Django View

Author Avatar wrote on 06/06/2022

Login required Function based

from django.contrib.auth.decorators import login_required

@login_required
def pricechecker(request):
Read more

Check If Form Input Field is Not Empty

Author Avatar wrote on 06/06/2022

If input fiels is not empty

if request.POST['Name']:
    news.name = request.POST['Name']
else:
    print('No name submitted')
If input filed name in request

if 'customer_id' in request.session:
    customer = Customer.objects.get(id=request.session['customer_id'])

Session and request

If in POST request

if 'amount' in request.POST:
    payment_amount = float(request.POST['amount'])
If in GET request

if 'amount' in request.GET:
    payment_amount = float(request.GET['amount'])
If in session

if 'customer_id' in request.session:
    customer = request.session['customer_id']
If in COOKIES

if 'username' in request.COOKIES and 'last_connection' in request.COOKIES:
      username = request.COOKIES['username']
Read more

Topics: TemplatesViews

CRUDgen Django CRUD generator