django跨站反向url

发布于 2024-08-27 20:29:53 字数 681 浏览 10 评论 0原文

我有一个与 django 跨站点反向类似的问题。但我认为我不能应用相同的解决方案。

我正在创建一个应用程序,让用户创建自己的网站。完成注册表单后,用户应该被重定向到其网站的新帖子表单。大概是这样的:

        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)

现在,我知道“new_post_url”很糟糕,会让婴儿哭泣,所以我需要以某种方式扭转它。我想使用 django.core.urlresolvers.reverse 来解决这个问题,但它只返回我的域上的网址,而不是用户新创建的网站中的网址,因此它对我不起作用。

那么,您知道更好/更聪明的方法来解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪都笑了 2024-09-03 20:29:53

看起来该域名是您自己网站的子域名,那么您无法反转该部分又有什么关系呢?使用 reverse 它不使用完整的域路径,它为您提供来自项目根目录的路径,因此您可以简单地执行以下操作:

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

这样您仍然使用反向,因此您不对网址进行硬编码(并让婴儿哭泣),据我所知,您并没有真正遇到问题。

最后,如果您不想在代码中硬编码您自己的域,请使用 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')))

如果您需要获取不带域的域使用 Sites 对象,最好的选择可能是 request.get_host(),它将获取完整的域和端口,但不是协议。

希望这能为你解释一些事情。您可以更好地格式化内容,但这就是要点。

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.

命比纸薄 2024-09-03 20:29:53

redirect 还可以选择将视图名称作为参数,因此,由于您已经拥有它所需的所有变量,因此只需传递带有所有必需参数的视图名称,然后就可以完成它,而不是尝试复杂的反转!

如果您仍然想要反向性质,可能您应该在站点模型上使用 get_absolute_url 。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文