Listing specific category first in Python - python

I have a list of categories from DB as following and it works fine + sorted by ID.
{% for category in menu_categories|sort(attribute="id"): %}
<div>
{{ category.name }}
</div>
{% endfor %}
I just need one exception if category='Pizza' exist to list it first.

Unless I misunderstood you, this should do it:
{% for category in menu_categories|sort(attribute="id"): %}
{% if category.name == 'Pizza': %}
<div> {{ category.name }} </div>
{% endif %}
{% endfor %}
{% for category in menu_categories|sort(attribute="id"): %}
{% if category.name != 'Pizza': %}
<div> {{ category.name }} </div>
{% endif %}
{% endfor %}

Related

Display error if there is error in fields

Im trying to raise error for empty fields or fields which are not validating in form
so Im doing this method below but I know this is not the best way...
views.py :
'KnowledgeForm': form,
'errors': str(form.errors),
but then in Django-template I have to use if for each field and im adding custom name for each field , i dont know why i cant use Verbose_name...
Template :
{% if errors %}
<div class="alert alert-danger">
<p>
{% if KnowledgeForm.errors.KnowledgeTitle %}
عنوان دانش: {{ KnowledgeForm.errors.KnowledgeTitle }}
{% endif %}
{% if KnowledgeForm.errors.KnowledgeTextSummary %}
Summary: {{ KnowledgeForm.errors.KnowledgeTextSummary }}
{% endif %}
{% if KnowledgeForm.errors.KnowledgeFromDate %}
from Date: {{ KnowledgeForm.errors.KnowledgeFromDate }}
{% endif %}
{% if KnowledgeForm.errors.KnowledgetoDate %}
To date : {{ KnowledgeForm.errors.KnowledgetoDate }}
{% endif %}
{% if KnowledgeForm.errors.KnowledgeProcess %}
Chart: {{ KnowledgeForm.errors.KnowledgeProcess }}
{% endif %}
{% endif %}
</p>
</div>
{% endif %}
Second method :
{% if KnowledgeForm.errors %}
<ul class="alert alert-danger">
{% for key,value in KnowledgeForm.errors.items %}
<li>{{ key|escape }} : {{ value|escape }}</li>
{% endfor %}
</ul>
{% endif %}
in this method i get the name based on whats used in models.py how can i change it?
The most clear and concise way is to use a forloop
Try replacing your entire if block in your HTML with the below code
{% for field in KnowledgeForm %}
{% if field.errors %}
<div class="alert alert-danger">
{{ field.label_tag }} {{ field.errors }}
</div>
{% endf %}
{% endfor %}
I don't think you need 'errors': str(form.errors),
It is because you convert errors to str and you don't need to separate this.
In your template:
# if you want to use verbose_name just use label_tag.
# label_tag is equal to your verbose name.
{% for field in KnowledgeForm %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
So you have the error top of input

Django : how to divide result from search

Assume I've created a search views with chain (multiple search from different app).
I want to display my result at the same place:
{% for object in object_list %}
{% with object|class_name as klass %}
{% if klass == 'Post' %}
{{ object.title }}
{% elif klass == 'Catego' %}
{{ object.name }}
{% elif klass == 'UserProfile' %}
{{ object.user.username }}
{% else %}
{% endif %}
{% endwith %}
{% empty %}
{% endfor %}
Everything is ok. I got all of my result.
Now, if I want to separate this result in different div (row and collapse class).
If I try this:
<div class="row collapse" id="collapseNutri">
{% for object in object_list %}
{% with object|class_name as klass %}
{% if klass == 'Post' %}
{{ object.title }}
{% endif %}
{% endwith %}
{% endfor %}
</div>
<div class="row collapse" id="collapseCatego">
{% for object in object_list %}
{% with object|class_name as klass %}
{% if klass == 'Catego' %}
{{ object.name }}
{% endif %}
{% endwith %}
{% endfor %}
</div>
<div class="row collapse" id="collapseUserProfile">
{% for object in object_list %}
{% with object|class_name as klass %}
{% if klass == 'UserProfile' %}
{{ object.user.username }}
{% else %}
{% endif %}
{% endwith %}
{% empty %}
{% endfor %}
</div>
I only got the Post result... I'm beginning with Django. Does anyone has an idea to separate search result in order to add different html presentation?

Unable to show Error with self.add_error()

I am having a problem and need your help.
I am making a django website, following information need to be verified. If it pass registration will be success and fail notice will show up. But Mean while there are no problem with success, but Notice won't show up if fail. Here are my form.py, could anyone help me? Much Thanks.
if settings.VERIFY_BY_SFID and all([i in cleaned_data for i in fields]):
number = cleaned_data["mobile_number"]
verified =SignUpFailureAttemptService.verify_mobile_number(number)
if not verified:
self.add_error(None, _("Your application was rejected."))
Template
{% extends 'accounts/login_base.html' %} {% load i18n %} {% load staticfiles %} {% load semanticui %} {% load common_templatetags %} {% block welcome_text %} {% trans 'Create your personal
E-wallet account.' %} {% endblock %} {% block slogan_text %} {% trans 'Buy and sell with protection
Easy and secure to send and receive money' %} {% endblock %} {% block bigger_text %} {% blocktrans %}Already have an account?{% endblocktrans %} {% trans 'Sign in' %} {% endblock %} {% block left_content %}
STEPS:
{% include 'accounts/signup/steps.html' with active_step=step steps=5 %}
{% block form_title %} {{ form_title }} {% endblock %}
{% if messages %} {% for message in messages %}
{{ message }}
{% endfor %} {% endif %}
{% block form_block %}
{% csrf_token %} {{ form.non_field_errors }} {% for hidden_field in form.hidden_fields %} {{ hidden_field.errors }} {{ hidden_field }} {% endfor %} {% for field in form.visible_fields %} {% if field.name == 'date_of_birth' %}
{{ field.label }}*
{% endif %}
{% if field.name == 'tos' %} {% blocktrans %} I agree to {% endblocktrans %} PDS and FSG {{ field }}
{% else %} {{ field }} {% endif %}
{{ field.help_text }}
{{ field.errors }}
{% endfor %}
{% if prev_step %} «   {% trans 'Back' %} {% endif %}
{% if next_step_title %}
{% trans 'NEXT STEP:' %} {{ next_step_title }}
{% endif %}
{% endblock %} {% endblock %} {% block scripts_end %} {% endblock %}

Form validation: how to get the actual length of a field shown in errors?

My form in Flask WTF looks like this:
class PublishForm(Form):
tweet = TextAreaField('tweet', [validators.DataRequired(), validators.Length(123, 123)])
When I show the error in template, I don't get the actual length shown. Any idea how to achieve this?
<div class="alert alert-danger">
{% for field in form.errors %}
{% for error in form.errors[field] %}
{{ error }}
{% endfor %}
{% endfor %}
</div>
I ended up fixing it like this:
{% if form.errors %}
<div class="alert alert-danger">
{% set count = form.tweet.data|length %}
{% for field in form.errors %}
{% for error in form.errors[field] %}
{{ error }}
Actual Length: {{ count }}
{% endfor %}
{% endfor %}
</div>
{% endif %}

How do I remove the bottom bullet from django table

I generate the following table using django_table2
Semester Actual Prediction
Spring 2014 209 199
*1 coe
The semester, actual, and prediction are column names. My table is exactly how i need it. However, at the bottom of each table I always have the number of items in my model. I do not want that bullet. I know its default in django_table2. Is there a way to remove this? Below is another example:
Department Semester Actual Prediction
MIE 2014 Spring 202 210
MIE 2015 Fall 213 200
MIE 2015 Spring 11 12
*3 departments
The easiest way to do it (for me) is by creating this path (your_templates_dir/django_tables2/tables.html) on your templates directory with the following content:
{% spaceless %}
{% load django_tables2 %}
{% load i18n %}
{% if table.page %}
<div class="table-container">
{% endif %}
{% block table %}
<table{% if table.attrs %} {{ table.attrs.as_html }}{% endif %}>
{% nospaceless %}
{% block table.thead %}
<thead>
<tr>
{% for column in table.columns %}
{% if column.orderable %}
<th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
{% else %}
<th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
{% endif %}
{% endfor %}
</tr>
</thead>
{% endblock table.thead %}
{% block table.tbody %}
<tbody>
{% for row in table.page.object_list|default:table.rows %} {# support pagination #}
{% block table.tbody.row %}
<tr class="{{ forloop.counter|divisibleby:2|yesno:"even,odd" }}"> {# avoid cycle for Django 1.2-1.6 compatibility #}
{% for column, cell in row.items %}
<td {{ column.attrs.td.as_html }}>{% if column.localize == None %}{{ cell }}{% else %}{% if column.localize %}{{ cell|localize }}{% else %}{{ cell|unlocalize }}{% endif %}{% endif %}</td>
{% endfor %}
</tr>
{% endblock table.tbody.row %}
{% empty %}
{% if table.empty_text %}
{% block table.tbody.empty_text %}
<tr><td colspan="{{ table.columns|length }}">{{ table.empty_text }}</td></tr>
{% endblock table.tbody.empty_text %}
{% endif %}
{% endfor %}
</tbody>
{% endblock table.tbody %}
{% block table.tfoot %}
<tfoot></tfoot>
{% endblock table.tfoot %}
{% endnospaceless %}
</table>
{% endblock table %}
{% if table.page %}
{% with table.page.paginator.count as total %}
{% with table.page.object_list|length as count %}
{% block pagination %}
<ul class="pagination">
{% if table.page.has_previous %}
{% nospaceless %}{% block pagination.previous %}<li class="previous">{% trans "Previous" %}</li>{% endblock pagination.previous %}{% endnospaceless %}
{% endif %}
{% if table.page.has_previous or table.page.has_next %}
{% nospaceless %}{% block pagination.current %}<li class="current">{% blocktrans with table.page.number as current and table.paginator.num_pages as total %}Page {{ current }} of {{ total }}{% endblocktrans %}</li>{% endblock pagination.current %}{% endnospaceless %}
{% endif %}
{% if table.page.has_next %}
{% nospaceless %}{% block pagination.next %}<li class="next">{% trans "Next" %}</li>{% endblock pagination.next %}{% endnospaceless %}
{% endif %}
</ul>
{% endblock pagination %}
{% endwith %}
{% endwith %}
</div>
{% endif %}
{% endspaceless %}
This approach will override all of the tables you create via django_table2 template tag and won't necessary work with upcoming versions of django_table2, but I'll be OK if you want to keep an specific version (in my case 0.15.0).
Another approach, which is more scalable, should work with future versions (and I recommend) is by subclassing Table, like described on the django-tables2 documentation.

Categories

Resources