开发者

How do I add a form value to a URL using Django

开发者 https://www.devze.com 2023-03-02 04:16 出处:网络
I have a simple form. It has a field that accepts a hostname and a submit button. When the form is submitted my app gets some info about the host provided and display it to the user.

I have a simple form. It has a field that accepts a hostname and a submit button. When the form is submitted my app gets some info about the host provided and display it to the user.

What I'd like to do is after the form is submitted have the URL updated to include the hostname provided by the user. For example:

http://my-host-checker.com

becomes

http://my-host-checker.com/acme.com

Also I would then like someone to be able to email the second URL as a link that can be used to automatically submit the request and get to the results pa开发者_开发知识库ge.

I'm very much a Django newbie and would appreciate some code examples if this is indeed possible.


Look at the documentation for Django URL dispatcher. It is pretty straight forward. For your example, you would probably need to use a catch-all regex like:

r'^(?P<site>.*)$'

I would suggest using a sub-folder, though, so that you can include other pages on your site. Something like:

r'^sites/(?P<site>.*)$'


The general idea is that you do a redirect after your form data is successfully processed.

Take a look at this example (which even shows how to send a mail).

Now, instead of redirecting to a static page (HttpResponseRedirect('/thanks/')) you should redirect to the detail view of your newly created object.

Assumed you already have a view setup for the detail pages of your hosts you can direct the user to the get_absolute_url() of the newly created object (check the permalink docs for more information).

HttpResponseRedirect(new_host.get_absolute_url())
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号