[Fixed]-How to Stay DRY in Django

1👍

I think what you’re doing is pretty much the standard way of doing it. At least from what I gather in the documentation.

It seems that defining the names as strings is what throws you off. My understanding is that it allows interesting uses like namespacing, transparent use across templates and views and saving you from importing everywhere you use it.

As @Chris points out, what you’re doing in urls.py is defining the url name, and then in views and templates you use it.

You could probably add more levels of indirection by defining:

my_url_name = 'password_changed'

but I don’t see how that is DRYer. When reversing the url resolution, you’d need:

reverse('Foo:' + my_url_name)

which isn’t much nicer.

In short, think of it as a variable name in quotes. If you need to rename it, you’ll have the exact same problem as when renaming a variable.

Leave a comment