Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: Best Practice for URL conf, url template tag
    primarykey
    data
    text
    <p>WIth class-based views having become MUCH better in Django, I am running into a "best practices" problem when implementing a class based view. It basically comes down to the <a href="https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#url" rel="noreferrer">URL template tag</a>.</p> <p>Given a urls.py like this: </p> <pre><code>urlpatterns = patterns('some_app.views', url(r'^$', 'index', name='some_app_index') ) </code></pre> <p>That tag can take either a path to a view: </p> <pre><code>{% url some_app.views.index %} </code></pre> <p>or the name of a url:</p> <pre><code>{% url some_app_index %} </code></pre> <hr> <p>Now, with a class-based url conf, one ends up with a url like this:</p> <pre><code>from some_app.views import Index urlpatterns = patterns('', url(r'^$', Index.as_view(), name='some_app_index') ) </code></pre> <p>Which means that using <code>{% url some_app.views.index %}</code> no longer works but <code>{% url some_app_index %}</code> still does. (And <code>{% url some_app.views.Index.as_view %}</code> doesn't seem to be a solution).</p> <hr> <p>So, my question is, what is best practice for refering to URL confs from a template? </p> <p>To this point, I beleived that using the path.to.view method was better, since it was cleanly namespaced. However, with class-based views looking better and better, is using the <a href="https://docs.djangoproject.com/en/1.3/topics/http/urls/#naming-url-patterns" rel="noreferrer">url name</a> a better way to go? In that case, namespacing is completely dependent on the name attribute being setup by the app developer in a way that separates the url name from other apps...</p> <p>Thoughts? I couldn't find a "do it this way" in the Django documentation but if anyone has written about this, I'd love to read it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload