Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Clarification of the question: "In a view, how do you get the name of a urlpattern that points to it, assuming there is exactly one such urlpattern."</p> <p>This may be desirable for the reasons stated: from within the view, we want a DRY way of getting a url to the same view (we don't want to have to know our own urlpattern name).</p> <p>Short answer: It's not really simple enough to teach your class, but it might be a fun thing for you to do in an hour or two and throw up on GitHub.</p> <p>If you really wanted to do this, you would have to subclass RegexURLResolver and modify the resolve method to return the matched pattern (from which you can get the pattern name) instead of the view and keyword/value pairs. <a href="http://code.djangoproject.com/browser/django/trunk/django/core/urlresolvers.py#L142" rel="nofollow noreferrer">http://code.djangoproject.com/browser/django/trunk/django/core/urlresolvers.py#L142</a></p> <p>You could then create a decorator or perhaps more appropriately middleware that uses this subclass to get the pattern name and store that value somewhere in the request so that views can use it.</p> <p>If you actually want to do that and you run across some trouble, let me know and I can probably help.</p> <p>For your class, I would just have them hardcode the pattern name in the view or template. I believe this is the acceptable way of doing it.</p> <p><strong>Update:</strong> The more I think about this, the more I would discourage trying to get a urlpattern name in a view. urlpattern parameters are fairly independent of the parameters of the view they point to. If you want to point to a certain url, you need to know how the urlpattern works, not just how the view works. If you need to know how the urlpattern works, you might as well have to know the name of the urlpattern.</p>
 

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