0

I am using Django for making a website. I am using an HTML form with GET as the method.

The problem is that by default the get url is like this: /search?name=user&place=place

But I want it to be something like: my_site/search/user/place

How can that be done?

Prince Kumar
  • 388
  • 1
  • 4
  • 15
  • Related topic: http://stackoverflow.com/q/1451138/821594 – stalk Oct 02 '14 at 12:02
  • These questions are not related. While the OP of other question wants to know how he can find out the domain name of the website, I want to have a clean looking url for a GET request. – Prince Kumar Oct 02 '14 at 12:47

4 Answers4

1

Why not use POST as method and retrieve the parameters in your view from request.POST? In this way they won't appear in your url.

Also, if you're expecting a list of results i recommend using ListView from views.generic, and in the dispatch() method you'll retrieve your parameters based on which you'll filter the user model (i guess).

Alex
  • 2,356
  • 15
  • 19
1

It is better with a get request immo, but if you want something like: my_site/search/user/place it is easy, you just have to define the variables in your url and get the arguments in your function.

You can find more detail in django documentation

cdvv7788
  • 2,021
  • 1
  • 18
  • 26
  • I am using that only for GET. I am able to take out the arguments from the URL and use it in views. But it will work only when I explicitly use that URL for getting the page. Problem arises when I wish to get a request using HTML forms. By default they construct the URLs like my_site/search?name=user&place=place. – Prince Kumar Oct 02 '14 at 18:36
  • You could use a POST, get the arguments and redirect to the url you want...where your GET can serve that – cdvv7788 Oct 02 '14 at 18:51
0

The only way you can do this in the browser is with Javascript. You will need to build the URL from the form contents. There are many mistakes you can make around encoding the values for the URL. You should be asking why you want to do it this way instead of using the QUERY_PARAMS as the form is doing.

Decoding it with Django isn't that hard, they are just variables in the URL pattern, but unless you have some kind of earth shattering new technology, you should let the browser send them to you without using JS to handcraft the URL.

boatcoder
  • 17,525
  • 18
  • 114
  • 178
0

Using the GET method send data via the web page. This means that the URL can be copied and rechecked at any time.