2

AoA, I am trying to redirect to view with parameters but unable to get the parameters

here is the code, it is working fine if I use (r'^search/$',... ) instead

url.py

url(r'^search/\?item=(?P<item_id>\d+)/$', 'contacts.views.search_Page', name='search_view'),

views.py

def search_Page(request,item_id):                   #GET Method
    return redirect('home_view')

it gives me no error, but django unable to find the page or view(404 page), why?

Using the URLconf defined in TestApp.urls, Django tried these URL patterns, in this order:

^$
^home/$ [name='home_view']
^logout/$
^save/$
^edit/$
^create/$
^account/$
^callback$ [name='callback']
^profile/$ [name='profile_view']
^search/\?item=(?P<item_id>\d+)$ [name='search_view']
^get/(?P<article_id>\d+)/$
^static\/(?P<path>.*)$
The current URL, search/item=4, didn't match any of these.

2 Answers 2

3

Paths go in urls.py. Handling query parameters goes in the view.

url.py

url(r'^search/$', 'contacts.views.search_Page', name='search_view'),

views.py

def search_Page(request):
    address = request.GET['item'] # or GET.get('item', '') if the parameter might not exist
    print address
Sign up to request clarification or add additional context in comments.

5 Comments

then how can I pass parameter from the some previous view?
i want to use query string from template search/?item=5 like this
Then use request.GET.item in the template. Make sure you have django.core.context_processors.request.
The URL that you redirect to must contain what ever query parameters want. You can do this from this view.
first of all, thanks for your response. Its another headache to find the parameter, first I have to find the specific view I am calling, like the url.py is unable to find search view, if passed with parameter( look at the debug log above) but when I use the search view without any parameter, works fine
0

Wrong regexp in urls.py. If you want to handle urls like search/?item=123/. Use this:

^search/\?item=(?P<item_id>\d+)/$

But better to use following with your view:

^search/(?P<item_id>\d+)/$

3 Comments

But original url was search/?item=123. I mean with '?'
it not working with any 127.0.0.1/search, nor with 127.0.0.1/search/?item=5
It works with 127.0.0.1/search/?item=5/ If you want 127.0.0.1/search/?item=5 - then use ^search/\?item=(?P<item_id>\d+)$

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.