如何在处理 django 信号后显示模板/重定向到模板?

发布于 2024-11-24 15:31:11 字数 412 浏览 2 评论 0 原文

我正在尝试集成 django-paypal,我需要处理成功或失败的信号。

我已经完成了所有代码,但现在需要显示一个模板,告诉用户他们的付款已成功或失败。

如果我 返回 HttpResponseRedirect... 什么也不会发生,并且我无法 render_to_response 因为我无权访问上下文(并且我正在使用 sekazai 或其他东西)。

我该怎么做?

## Called when django-paypal fails to validate PDT data
def pdt_failed_transaction(sender, **kwargs):
    return HttpResponseRedirect(reverse('payment-error'))

I'm trying to integrate django-paypal, and I need to process a successful or failed signal.

I've got all my code done, but I now need to display a template telling the user their payment has succeeded or failed.

If I return HttpResponseRedirect... nothing happens, and I can't render_to_response because I don't have access to the context (and I'm using sekazai or something).

How can I do this?

## Called when django-paypal fails to validate PDT data
def pdt_failed_transaction(sender, **kwargs):
    return HttpResponseRedirect(reverse('payment-error'))

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

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

发布评论

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

评论(2

烂人 2024-12-01 15:31:11

简短的回答:你不能。这不是信号的设计目的。您需要将验证代码添加到表单中并在视图中处理响应。就是这样的过程。

Short answer: you can't. This isn't what signals are designed for. You'll need to work the validation code into your form and handle the responses in your view. That's the process.

允世 2024-12-01 15:31:11

通过谷歌搜索发现这个问题,发现答案非常无用/无帮助。我认为这是一个合理的好奇心,虽然 Chris Pratt 是正确的,您不能修改信号中的请求或响应,但想要基于刚刚登录的用户运行一些代码似乎确实合理。

仅在以下情况下运行某些代码有人登录时包含重定向,您可以使用中间视图和 settings.py 中的 LOGIN_REDIRECT_URL 设置 https://docs.djangoproject.com/en/dev/ref/settings/#login-redirect-url

在settings.py中:

LOGIN_REDIRECT_URL = reverse_lazy('app.views.after_login')

在app/views.py中:

@login_required
def after_login(request):
    if conditions met:
        return HttpResponseRedirect(reverse('home_alt'))
    else:
        return HttpResponseRedirect(reverse('home'))

Came across this question through a Google search and found the response pretty useless/unhelpful. I think this is a valid curiosity, and while Chris Pratt is correct that you cannot modify the request or response in signals, it does seem reasonable to want to run some code based on a user having just logged in.

To run some code only when someone logs in that contains a redirect, you can use an intermediary view and the LOGIN_REDIRECT_URL setting in your settings.py https://docs.djangoproject.com/en/dev/ref/settings/#login-redirect-url

in settings.py:

LOGIN_REDIRECT_URL = reverse_lazy('app.views.after_login')

in app/views.py:

@login_required
def after_login(request):
    if conditions met:
        return HttpResponseRedirect(reverse('home_alt'))
    else:
        return HttpResponseRedirect(reverse('home'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文