JavaEar 专注于收集分享传播有价值的技术资料

GET data in django urls.py not passing to view

I'm having a tough time figuring out how to parse my URL GET data and send it to the right view.

I have a search view that only has a search input:

template/search.html

<form action="http://domain.com/schools/search/" method="GET" >
    <input type="text" name = "q_word">
    <input type="submit" value="Search"/>
</form>

When a user enters in a search term, I want to send that data to another view to parse and use in a geocoding function I wrote. Here is a look at my urls.py:

url(r'^schools/search/$', school_views.find_school, name="find_school"),
url(r'^schools/search/(?P<address>[\w ]+)$', school_views.geo_locate, name="geo_locate"),

I want to grab the GET data from a URL (after they've entered in the info), and pass it as an address argument to my school_views.geo_locate function.

This set up works great when you manually type out a URL like: schools/search/150%20main%20Street

But when a user submits any form data, the URL passed is /schools/search/?q_word=150+west+main and I'm just kicked back to my search template.

I think my regex needs to be tweaked in my second url argument, but I just keep returning to the search page after submission, with no data going to my geo_locate view. Is this a URLs problem?

1个回答

    最佳答案
  1. GET data is not passed in the URL parameters. Don't try to capture it in the regex. Just get it from request.GET inside the view.