Comparing reversed URLs in Django template

In Django template we can use ‘url’ template tags to get path to a view, like this:

{% url 'blog:top' %}

It may render a ‘/blog/’ text.

How can we compare this returned path? You can use ‘as’ syntax on url template tag to save the returned value to a veriable, and compare it with some string or other variables:

{% url 'blog:top' as top_path %}

compareing with string.
{% if top_path == '/blog/'%}The path to blog top was '/blog/'{% endif %}

with other variables.
{% if top_path == request.path %}You are at the top of my blog{% endif %}

But generally, this tip is not so smart way. Because templates should not have many logics and know about others.

Let’s consider some anti-pattern for this tip. If you want not to render some text if a user is in the specific page, you can write this template:

{# In base.html #}
<html>
<body>
<a href="{% url 'top' as top_path %}
         {% if top_path != request.path %}
             {{ top_path }}
         {% endblock %}">Bland Logo</a>
{% block content %}{% endblock %}
</body>
</html>
{# In top.html #}
{% extends 'base.html' %}
{% block content %}
    Hello This is top page.
{% endblock %}

This ‘if’ syntax in base.html means “Don’t render the path for top page if a user is in the top page”. It might work correctly. But it can be relized by more simply way, using a block syntax:

{# In base.html #}
<html>
<body>
<a href="{% block bland_path%}{{ top_path }}{% endblock %}">Bland Logo</a>
{% block content %}{% endblock %}
</body>
</html>
{# In top.html #}
{% extends 'base.html' %}
{% block top_path %}{% endblock %}{# Overriding with empty text #}
{% block content %}
    Hello This is top page.
{% endblock %}

That’s it. In this, the child template know about the behavior for hiding the link for top page. In previous one, the template should access ‘request’, so you need to use ‘contextprocessors.request’. And it is painfull to add page not to render the link. So If you want to compare urls in django template, you might re-consider and notice it can be realised by inheritance.

New year’s python meme 2013

Today is the final day of 2013, time to write New year’s python meme. This is the last year’s one:

1. What’s the coolest Python application, framework or library you have discovered in 2013 ?

Kaa is the coolest application. This is a CUI editor written in Python3.3. It is easy to use and useful, so I changed to use it on a routine basis. The development of kaa is very active, a new version is available almost everyday. Check out the version history.

I’m checking a Webframe work named morepath, too.

2. What new programming technique did you learn in 2013 ?

I learned how to distribute packages, creating libraries and Web frameworks. And learned the basics of Python, HTTP and WSGI. Last year, I didn’t know about them. just I created own Web applications using some frameworks. I aimed to learn them, written in 2012’s python meme. All of things I created and contributed are available on next section.

3. Which open source project did you contribute to the most in 2012 ? What did you do ?

May be Django. This year I joined one of django authors.

Or, Uiro framework.

And I created many libraries this year:

I also contributed existed projects:

  • django: Adding a minor feature for django 1.6 with coolRR, reviewed by Claudep.
  • django-localflavor: Fixed a form providing Japanese prefectures, reviewed by nagisa.
  • django-admin-tools: Adding a test for Django 1.6.
  • pyramid_layout: Fixed bug on Panels withoud Layounts, reviewed by tseaver and chrisrossi.

4. Which Python blog or website did you read the most in 2013 ?

In Japanese:

5. What are the three top things you want to learn in 2014 ?

  1. English:
Next year, I will join to EuroPython2014. This will be the first time to join another country’s event. I will learn about English in order to enjoy this event much more. I listen to VOA Leaning English these days.
  1. How to keep developing and improving one project:
This year I created many packages, but almost of them are left alone. And I learned that the first release is not so important. To create effective and useful software, it is necessary to keep creating and improving.
  1. Not only Web. I’ve learned about Web programming for about 2 years:
This year, I will try to create something not relating Web. For the first step, now I’m creating genaa, a command line tool.

6. What is the top software, application or library you wish someone would write in 2014 ?

genaa is.

And some Web applications or libraries for Pyramid, not only Django.

I learned many things in 2013. I wish I would write more interesting things in 2014. Have a good holiday.