Django Python Script
<p>I've been sifting through the documention but I don't feel like I'm getting a clear answer.  Is it possible to run something python-like</p>

<pre><code>if company_name.startswith(('A')):
enter code here
</code></pre>

<p>from within a Django site or app?  How would I go about it?</p>

<p>Currently the code I use is </p>

<pre><code>            {% for TblCompanies in object_list %}
              &lt;tr class="customer-table"&gt;
                &lt;td&gt;{{ TblCompanies.company_id }}&lt;/td&gt;
                &lt;td&gt;{{ TblCompanies.company_name }}&lt;/td&gt;
                &lt;td&gt;{{ TblCompanies.contact_phone }}&lt;/td&gt;
                &lt;td&gt;{{ TblCompanies.billing_address }}&lt;/td&gt;
                &lt;td&gt;{{ TblCompanies.contact_e_mail }}&lt;/td&gt;
              &lt;/tr&gt;
            {% endfor %}
</code></pre>

<p>but our customer database is too large and it's a burden to have to go through the list to find a customer.  I want to instead sort it alphabetically using urls like <a href="http://path/to/customer_list/A" rel="nofollow">http://path/to/customer_list/A</a></p>

<p>Using <a href="https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#slice" rel="nofollow"><code>slice</code> filter</a>, you can get a substring; then compare the substring with the <code>'A'</code>:</p>

<pre><code>{% for TblCompanies in object_list %}
  {% if TblCompanies.company_name|slice:':1' == 'A' %}
  &lt;tr class="customer-table"&gt;
    &lt;td&gt;{{ TblCompanies.company_id }}&lt;/td&gt;
    &lt;td&gt;{{ TblCompanies.company_name }}&lt;/td&gt;
    &lt;td&gt;{{ TblCompanies.contact_phone }}&lt;/td&gt;
    &lt;td&gt;{{ TblCompanies.billing_address }}&lt;/td&gt;
    &lt;td&gt;{{ TblCompanies.contact_e_mail }}&lt;/td&gt;
  &lt;/tr&gt;
  {% endif %}
{% endfor %}
</code></pre>

<p>As @Matthias commented, it would be better to pass filtered <code>object_list</code> in view. Assuming <code>object_list</code> is a queryset object:</p>

<pre><code>object_list = object_list.filter(company_name__startswith='A')
</code></pre>

<hr>

<h2>Sorintg</h2>

<p>Sort the <code>object_list</code> before passing it to the template:</p>

<pre><code>page = requests.REQUEST.get('page', 'A')  # or Get from view parameter
                                          #    depending on url conf.
object_list = (object_list.filter(company_name__startswith=page)
                          .order_by('company_name'))
</code></pre>

<p><strong>UPDATE</strong></p>

<p>NOTE: Change <code>app</code> with actual app name.</p>

<p>urls.py:</p>

<pre><code>url(r'^/path/to/site/customers/(?P&lt;page&gt;[A-Z])$', 'app.views.list_customers')
</code></pre>

<p>app/views.py:</p>

<pre><code>from django.shortcuts import render

def list_compnaies(request, page):
    object_list = (Customer.objects.filter(company_name__startswith=page)
                           .order_by('company_name'))
    return render(request, 'customers/list.html', {
        'object_list': object_list,
    })
</code></pre>

<p>customers/list.html</p>

<pre><code>{# Link to A .. Z customer pages %}
{% for page in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' %}
&lt;a href="/path/to/site/customers/{{ page }}"&gt;{{ page }}&lt;/a&gt;
{# Use {% url ... %} once you learn the url tag if possible to reduce duplicated hard-coded url #}
{% endif %}

{% for TblCompanies in object_list %}
&lt;tr class="customer-table"&gt;
    &lt;td&gt;{{ TblCompanies.company_id }}&lt;/td&gt;
    &lt;td&gt;{{ TblCompanies.company_name }}&lt;/td&gt;
    &lt;td&gt;{{ TblCompanies.contact_phone }}&lt;/td&gt;
    &lt;td&gt;{{ TblCompanies.billing_address }}&lt;/td&gt;
    &lt;td&gt;{{ TblCompanies.contact_e_mail }}&lt;/td&gt;
&lt;/tr&gt;
{% endfor %}
</code></pre>

python django django-templates django-views django-urls