I have a similar question than django cross-site reverse. But i think I can't apply the same solution.
我有一个类似于django跨站点反向的问题。但我认为我不能应用相同的解决方案。
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.
现在,我知道“new_post_url”非常糟糕,让婴儿哭了所以我需要以某种方式扭转它。我想在使用django.core.urlresolvers.reverse来解决这个问题,但是只返回我的域名上的url,而不是用户新创建的站点中的url,所以它对我不起作用。
So, do you know a better/smarter way to solve this?
那么,你知道更好/更聪明的方法来解决这个问题吗?
2 个解决方案
#1
1
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:
最后,如果您不希望在代码中对您自己的域进行硬编码,请使用Django的Sites模型来获取当前站点,请确保将其从默认的example.com修改为您自己的域,因此最终您的代码可以是:
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.
如果您需要在不使用Sites对象的情况下获取域名,最好的选择可能是request.get_host(),它将获得完整的域名加端口,但不会获得协议。
Hopefully that explains things for you. You can format things a bit better, but that's the gist of it.
希望这能为您解释一些事情。你可以更好地格式化事物,但这是它的要点。
#2
0
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!
redirect也可以选择将视图名称作为参数,因此,既然你已经拥有了它所需的所有变量,只需传递带有所有必需参数的视图名称,并完成它,而不是尝试复杂的反向!
If you still want a reverse nature, may be you should use, get_absolute_url on the model of the Site.
如果您仍然需要反向性质,可能应该在网站模型上使用get_absolute_url。
#1
1
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:
最后,如果您不希望在代码中对您自己的域进行硬编码,请使用Django的Sites模型来获取当前站点,请确保将其从默认的example.com修改为您自己的域,因此最终您的代码可以是:
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.
如果您需要在不使用Sites对象的情况下获取域名,最好的选择可能是request.get_host(),它将获得完整的域名加端口,但不会获得协议。
Hopefully that explains things for you. You can format things a bit better, but that's the gist of it.
希望这能为您解释一些事情。你可以更好地格式化事物,但这是它的要点。
#2
0
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!
redirect也可以选择将视图名称作为参数,因此,既然你已经拥有了它所需的所有变量,只需传递带有所有必需参数的视图名称,并完成它,而不是尝试复杂的反向!
If you still want a reverse nature, may be you should use, get_absolute_url on the model of the Site.
如果您仍然需要反向性质,可能应该在网站模型上使用get_absolute_url。