0

I'm constructing the urlconf for a website but I'm facing a couple of issues;

At some point I have to present a list of tasks which can eventually be reordered. The problem is that I have no clue on how to go about configuring the regex.

So far I've got this:

url(r'^myapp/mytasks, myview.tasks),

The default behavior is to accept requests at www.mydomain.com/myapp/mytasks.

However should the order button be pressed by a user, I need Django to also accept requests of type:

www.mydomain.com/myapp/mytasks/sort_by/price_highest www.mydomain.com/myapp/mytasks/sort_by/price_lowest

but not

www.mydomain.com/myapp/mytasks/price_lowest

Is there a regex for this scenario?

Thank you all in advance.

P.S Ideally I would also like to know how I could possibly reverse them.

6
  • try url(r'^myapp/mytasks/sort_by', myview.tasks), Commented Mar 6, 2015 at 13:55
  • @AvinashRaj This won't work for the default scenario: www.mydomain.com/myapp/mytasks. Commented Mar 6, 2015 at 13:57
  • 1
    For options such as sorting, I'm more inclined to use a query string such as www.mydomain.com/myapp/mytasks?sort_by=price_highest, or even www.mydomain.com/myapp/mytasks&sort_by=price&reversed=false. You'd then have to process the query string inside the view. Commented Mar 6, 2015 at 13:58
  • @Konos5 try url(r'^myapp/mytasks(?!/price_lowest)', myview.tasks) Commented Mar 6, 2015 at 14:01
  • @AvinashRaj Thanks but I think you are missing the point here. It's not that I need everything except price_lowest. I need price_lowest or price_highest or whatever other parameter comes to my mind but only after /sort_by/ exists. If sort_by is absent, discard all and revert to default. Commented Mar 6, 2015 at 14:06

1 Answer 1

1

Create two url records for the same view:

url(r'^myapp/mytasks/$', myview.tasks, name='tasks'),
url(r'^myapp/mytasks/sort_by/(price_highest|price_lowest)/$', myview.tasks,
                                                   name='sorted_tasks'),

And then change the signature of the tasks() view to:

def tasks(request, sort_by=None):
    ...

In the template you can easily point to the both versions:

{% url 'tasks' %}
{% url 'sorted_tasks' 'price_highest' %}

UPDATE: If you really want to make the only one url record then the regex will be:

'^myapp/mytasks/(?:sort_by/(price_highest|price_lowest)/)?$'

But I am not sure that it will work with {% url %} tag.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I am aware of the multiple url records. However I need to do this in a single url record using only regex so I can easily reverse it in my templates.
What is the problem with reversing of two urls? See the updated answer.
As you put it... there is no problem and your solution seems to be working perfectly fine. However it is a matter of personal preference. And I would really like to see this tricky regex that would accomodate my problem.
The regex is not so tricky but I never used something like this in url and not sure that the reverse for this regex will work. You can test it if you want. See the updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.