Django URLS, using a ? in the URL
Asked Answered
F

3

7

I am trying to do some Django URL matching.

i want a few urls where i have http://mysite.com/base?sort=type1/, http://mysite.com/base?sort=type2/, etc.

I can't figure out how to URL match these expressions: I'm very new to Django and never used Reg Ex before.

What I have for urls.py in my "base" application is:

url(r'^$','base.views.main, name='main'),

I can't figure out what to put to match my urls with question marks.

I'm trying something like

url(r'^?sort=popular/$', 'base.views.main_popular', name='main_popular'),

Thanks for help!

Freshwater answered 22/8, 2012 at 18:46 Comment(0)
S
6

? won't match an "?" inside the url , instead it has its own meaning which you can look it up here :
Python Regular Expressions If you want to match the exact character of "?" inside your url , you have to somehow escape it ( cause it has a meaning in RegExs ) so you might wanna escape it by a "\" (a backslash ) so you would write \?sort ....

EDIT :
Okay so with what you've said in comments , seems here's your problem , main?sort=popular occurs on your url pattern when you are rendering the template for /main/ with the GET method dictionary argument of sort=popular, just write a function that distinguishes between GET and POST , in the GET part , have sth like sort_by = request.GET.get('sort','') and then sort accordingly with the value of sort_by variable, would be sth like :

def main_handler(request):
     if request.method == "POST":
           whatever ... 
     if request.method == "GET" :
           sort_by = request.GET.get('sort','')
           if sort_by:
                 sort by what sort points to 
                 return "the sorted template"
     return render_to_response(the page and it's args)

and let go of that ? inside the url pattern , that's added when you request a page with a GET argument.

Sphygmic answered 22/8, 2012 at 19:0 Comment(3)
I tried doing this, unfortunately it didn't really work out--I'm not quite sure why.Freshwater
you might wanna post that code too ? but really , it seems so weird to have a question mark in the beginning of such a url ( sort=popular )Sphygmic
Well essentially I have one webpage, with content that can be sorted based on the sort. main?sort=popular sort of implies that you're still on the main page, vs main/popular/ which kind of implies a different page. I was using url(r'^\?sort=popular/$', 'base.views.main_popular', name='main_popular')Freshwater
F
8

You don't match these against the regex. The elements after the ? are not part of the URL, they are query parameters which can be accessed from your view via request.GET.

Flacon answered 22/8, 2012 at 18:50 Comment(0)
S
6

? won't match an "?" inside the url , instead it has its own meaning which you can look it up here :
Python Regular Expressions If you want to match the exact character of "?" inside your url , you have to somehow escape it ( cause it has a meaning in RegExs ) so you might wanna escape it by a "\" (a backslash ) so you would write \?sort ....

EDIT :
Okay so with what you've said in comments , seems here's your problem , main?sort=popular occurs on your url pattern when you are rendering the template for /main/ with the GET method dictionary argument of sort=popular, just write a function that distinguishes between GET and POST , in the GET part , have sth like sort_by = request.GET.get('sort','') and then sort accordingly with the value of sort_by variable, would be sth like :

def main_handler(request):
     if request.method == "POST":
           whatever ... 
     if request.method == "GET" :
           sort_by = request.GET.get('sort','')
           if sort_by:
                 sort by what sort points to 
                 return "the sorted template"
     return render_to_response(the page and it's args)

and let go of that ? inside the url pattern , that's added when you request a page with a GET argument.

Sphygmic answered 22/8, 2012 at 19:0 Comment(3)
I tried doing this, unfortunately it didn't really work out--I'm not quite sure why.Freshwater
you might wanna post that code too ? but really , it seems so weird to have a question mark in the beginning of such a url ( sort=popular )Sphygmic
Well essentially I have one webpage, with content that can be sorted based on the sort. main?sort=popular sort of implies that you're still on the main page, vs main/popular/ which kind of implies a different page. I was using url(r'^\?sort=popular/$', 'base.views.main_popular', name='main_popular')Freshwater
H
0

You don't need to this. Instead you can make a common template and view.

#And this is the views.py
def main_handler(request):
    if request.method == "GET":
        sort_parameter = request.GET.get('sort')
        if sort_parameter:
             #the code to sort the database objects on basis of the sort parameter
             return render(The template and its kwargs)
        #your other code

Your urls file should look like:

urlpatterns = [
url(r'base/', 'base.views.thecommonview', name='main'),
]

Hypercorrection answered 22/10, 2020 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.