Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think it's as much work as you're making it out to be - you can use variables instead of explicitly creating separate url patterns. If you look at how the django admin handles it, they tack on request variables to the url like <code>?ot=asc&amp;o=2</code> This corresponds to sort in ascending order in by the 2nd column. Of course, if you designing a particular page, you might as well use more readable naming. So instead of numbering the categories, i'd do <code>?sort=desc&amp;order_by=date</code> and then put a regular expression in the view to match the different possibilities. Something like: </p> <pre><code>order = re.match(r"(?:date|time|name)$", request.GET['order_by']) if request.GET['sort'] == 'desc': order = '-' + order results = Event.objects.filter().order_by(order) </code></pre> <p>You could instead use the regexp as a url pattern matcher as you suggested, but it's more common to let the url itself represent which part of the site you're at (i.e. <code>website.com/events/</code>) and the url request variables represent how that content is being displayed (i.e. <code>?order_by=date&amp;sort=desc</code>).</p> <p>Hope that helps!</p> <p><strong>EDIT:</strong> For the second part of your question, use Django's templating system (which reads variables) instead of just html. There are several ways I can think of to do this, depending on personal preference and how exactly you want the UI to function (i.e. page loads with new variables anytime the user chooses a filter, or the user chooses all filter options in a form and then submits it so the page only has to reload once, etc). In this case, you could just do: </p> <pre><code>&lt;a href="?sort_by=asc&amp;order_by={{order}}"&gt;Ascending&lt;/a&gt; &lt;a href="?sort_by=desc&amp;order_by={{order}}"&gt;Descending&lt;/a&gt; &lt;a href="?sort_by={{sort}}&amp;order_by=name"&gt;Name&lt;/a&gt; &lt;a href="?sort_by={{sort}}&amp;order_by=date"&gt;Date&lt;/a&gt; </code></pre> <p>Then in the view make sure your render_to_response arguments include a dictionary that looks like: <code>{'order': request.GET['order_by'], 'sort': request.GET['sort_by'], }</code></p> <p>Unfortunately, (and someone please correct me if I'm wrong) I don't think there's a template tag to generate a url with request.GET parameters - the url tag <code>{% url name_of_view order_by=name sort_by=desc %}</code> would generate "path/to/name_of_view/name/desc/", but I don't think there's a tag to generate "path/to/name_of_view?order_by=name&amp;sort_by=desc". It would be pretty easy to write a custom tag for this though (I wouldn't be surprised if there's already one on django-snippets or something, although I just did a quick google search and didn't find anything).</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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