Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango url configuration for DRYness
    primarykey
    data
    text
    <p>Most views in my project accept an optional username parameter and if exists, filter the querysets passed to the templates for that user. So, for example:</p> <p>the index view handles both the following url patterns:</p> <pre><code>'^$' # general index page '^(?P&lt;username&gt;[-\w]+)/$' # index page for the user '^photos/$' # photo index page '^(?P&lt;username&gt;[-\w]+)/photos/$' # photos for that user ... </code></pre> <p>As there are a number of such applications, it doesn't seem very DRY to implement the same logic by replicating the patterns. I thought it may be possible to recursively include the main urls.py module, so I did this:</p> <pre><code>url(r'^(?P&lt;username&gt;[-\w]+)/', include('urls')), </code></pre> <p>My reasoning was that, when an other urls module is included, the matched pattern is removed from the path. So, I was hoping that </p> <pre><code>'^(?P&lt;username&gt;[-\w]+)/photos/$' </code></pre> <p>would become </p> <pre><code>'^photos/$' </code></pre> <p>when it is matched by the recursively included urls module, with the extra username parameter. But this caused the development server to die silently when a request is made.</p> <p>The second approach I can think of is to write a middleware, which would match the pattern in the url, if exists, and add the viewed user to the request and remove the part that matches the username from the request path. But I don't want to mess with the path as this may have unpredictable results. </p> <p>What would you recommend? Am I too picky for DRYness?</p> <p>Thanks, oMat</p>
    singulars
    1. This table or related slice is empty.
    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.
    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