0

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 page.

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

2
  • Usually everyone on here likes to see the original poster give it a shot first. Not many people just give you the answers. So give it a shot, post what you have and people usually will help you out. Commented Apr 29, 2011 at 15:40
  • Please read the starting tutorial. docs.djangoproject.com/en/dev/intro/tutorial01 . It will give you ideas on writing your urls.py, models.py, views.py etc. Commented Apr 29, 2011 at 15:44

2 Answers 2

2

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>.*)$'
Sign up to request clarification or add additional context in comments.

1 Comment

He needs to capture the second segment also: (?P<site>[.\w]+)
1

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())

3 Comments

Thanks. I don't actually read the host info from a database. Rather I connect to the hostname provided and grab it's data each time.
Okay. But the general approach (process form, send mail, redirect) should nonetheless be valid. Or are there still specific steps you're unsure about?
My plan is to get the hostname from the form and then redirect to the details view where I will connect to the host, get info, and display the result. This will also let the details page be called directly with a url containing the hostname. Just wanted to check that sort of fitted with the Django way? Thanks again.

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.