Skip to main content
 首页 » 编程设计

python之重定向到另一个域的 Django View

2024年09月07日36zhenyulu

如何使用 python django 重定向到另一个域并传递附加信息?

例如,我想重定向到 https://ssl.dotpay.pl/test_payment/ 并提供其他信息,因此 url 将如下所示 https://ssl.dotpay.pl/test_payment/?id=123456&amount=123.00&description=Test 但我不想在我的 View 中生成 url,只是将数据传递到 json 或类似的东西中。

有什么方法可以做到这一点?

请您参考如下方法:

this is generated url by what i meant 'ssl.dotpay.pl/test_payment/?id=123456&amount={}&description={}'.format(123.00, 'Test')

一定要在您的网址前加上协议(protocol)(或至少两个斜杠)。 这样,Django 会将其视为绝对路径。 从您的评论来看,您似乎重定向到 ssl.dotpay.pl,这将被视为本地路径而不是另一个域。

这是我遇到的。 (参见我放在 stackoverflow 上的 question 和 answer )

因此,在您的情况下,您可以使用以下内容:

class MyView(View): 
    def get(self, request, *args, **kwargs): 
        url = 'https://ssl.dotpay.pl/test_payment/' 
              '?id=123456&amount={}&description={}'.format(123.00, 'Test') 
        return HttpResponseRedirect(url) 

您还可以使用 django.shortcuts 中的 redirect 而不是 HttpResponseRedirect