I must have some sort of misconception of how GET variables can be manipulated in django, but here goes:

(1) I have a search form that has two required parameters, and one optional parameters.

The form sends to /search/ like so :

<form action="/search/" method="GET">

However, the urls that result from this look something like

http://mylifeforregex:8000/search/?keyword=keyword&columns=name&exact=on

I'm not able to match it in my urlpatterns with the following line :

url(r'^search/(?P<keyword>\w+)(?P<columns>\w+)(?P<exact>\w+)?$', 'home', name='searched')

So if someone could tell me what exactly I'm doing wrong with this regex, it'd be much appreciated.

(2) The more interesting question I have personally is : is there any way I can change the way the form data will be presented? Is it possible to have the form query result in a url that looks like

http://halp:8000/search/keywordquery/columnquery/optionmarked
有帮助吗?

解决方案

In this case, none of the querystring parameters that are appended to the URL will match your pattern, because the pattern is simply not the same.

You would need to pick those up in your 'home' view as such:

[variable] = request.GET.get([key])

To get the url pattern you're expecting, you'd have to do a POST to your view that handles the form to get the values, and then redirect to '/search/[keyword]/[column]/[exact]/'

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top