views:

158

answers:

2

I have a similar question than django cross-site reverse. But i think I can't apply the same solution.

I'm creating an app that lets the users create their own site. After completing the signup form the user should be redirected to his site's new post form. Something along this lines:

        new_post_url = 'http://%s.domain:9292/manage/new_post %site.domain'
        logged_user = authenticate(username=user.username, password=user.password)
        if logged_user is not None:
            login(request, logged_user)
            return redirect(new_product_url)

Now, I know that "new_post_url" is awful and makes babies cry so I need to reverse it in some way. I thought in using django.core.urlresolvers.reverse to solve this but that only returns urls on my domain, and not in the user's newly created site, so it doesn't works for me.

So, do you know a better/smarter way to solve this?

A: 

It looks like the domain is a subdomain of your own website so what does it matter that you can't reverse that part? Using reverse it doesn't use full domain paths, it gives you the path from the root of the project, so you can simply do something like:

new_post_uri = 'http://%s.domain:9292%s' % (site.domain, reverse('manage_new_post'))

This way you're still using reverse so you're not hardcoding the urls (and making babies cry) and you're not realy having an issue as far as I can see.

Finally, if you do not wish to hardcode your own domain in the code, uses Django's Sites model to get the current site, make sure to modify it from the default example.com to your own domain, so finally your code can be:

current_site = Site.objects.get_current() # See the docs for other options
return redirect('http://%s.%s%s' % (site.domain, current_site, reverse('manage_new_post')))

If you need to get the domain without using the Sites object, your best bet may be request.get_host(), which will get the full domain plus port, but not the protocol.

Hopefully that explains things for you. You can format things a bit better, but that's the gist of it.

Bartek
it's the *domain:9292* part of the URI that is bothering me. I don't know first hand in which domain the site will run. An in this context current_site does not necessarily has the servers domain, as I run the app in different installations (ie: the current_site domain can be stored as 'example.com' but I'm running the app in localhost:8000). I need to get it dinamically, hence the question
tutuca
so I didn't understand completely, then do you just need to fetch the current domain you can use `request.get_host()` which will fetch the current domain and the port, but not the protocol, so you'll have to say if you're using http:// or https://. Hope that h elps
Bartek
You can use `request.is_secure()` to determine the protocol. So you can use the line: `protocol = request.is_secure() and 'https' or 'http'`
jtes0111
A: 

redirect also optionally takes a view-name as the argument, So, since you already have all variables needed by it, just pass the view name with all the required arguments, and be done with it, rather than trying out a complicated reverse!

If you still want a reverse nature, may be you should use, get_absolute_url on the model of the Site.

Lakshman Prasad