Question: I'm learning django and python and I want to know how to indent this code properly. How should it be done?

```
{% block content %}
    <h2>Nyinkommet</h2>
    {% if request.GET.sorting == 'desc' %}
        <form method="get" action=".">
        <input type="hidden" name="sorting" value="asc">
        <input type="submit" value="Visa äldsta ärende först">
        </form>
    {% else %}
        <form method="get" action=".">
        <input type="hidden" name="sorting" value="desc">
        <input type="submit" value="Visa nyaste ärende först">
        </form>
{% endif %} 

```

Answer: 
You could use the template tags `{{ sortvalue }}` to check the value and set the specific attribute value.

You could achieve it somewhere as:

**my\_template.html**

```
{% block content %}
    <h2>Nyinkommet</h2>
    <form method="post" action="/postingUrl">
       <input type="hidden" name="sorting" value="{{ sortvalue }}">
       <input type="submit" value="Visa äldsta ärende först">
    </form>
{% endblock %}

```

Pass the sortvalue in the rendering of template:

**The view that returns "my\_template.html"**:

```
def get_home_page(request):
    sortvalue = "asc" # Calculate what value you want, (asc or desc)
    return render_to_response('my_template.html',
                          { 'sortvalue' : sortvalue },
                          context_instance=RequestContext(request))

```
Code indentation comes down to personal preference. As long as your code is readable it is up to you and those you work with; do what you want.

For ideas and general good practises you should look through the [django documentation](https://docs.djangoproject.com/en/1.8/ref/templates/language/#templates). It is contributed to by x00's of developers and will give you a good idea of formatting and best practices.

Personally I would indent the elements inside the forms. I also try to keep all HTML dom elements at the same nesting level as their siblings even when using django template operations.

```
{% block content %}
    <h2>Nyinkommet</h2>
    {% if request.GET.sorting == 'desc' %}
        <form method="get" action=".">
            <input type="hidden" name="sorting" value="asc">
            <input type="submit" value="Visa äldsta ärende först">
        </form>
    {% else %}
        <form method="get" action=".">
            <input type="hidden" name="sorting" value="desc">
            <input type="submit" value="Visa nyaste ärende först">
        </form>
{% endif %} 

```

One small improvement you could make to the code is the following:

```
{% block content %}
    <h2>Nyinkommet</h2>
    <form method="get" action=".">
        {% if request.GET.sorting == 'desc' %}
        <input type="hidden" name="sorting" value="asc">
        {% else %}
        <input type="hidden" name="sorting" value="desc">
        {% endif %}
        <input type="submit" value="Visa äldsta ärende först">
    </form>
{% endblock content %}

```