0

我正在使用django-registration. 现在,我正在尝试在registration_complete页面上创建一个 Paypal 表单。

我创建了一个会话变量,如下所示:

def user_created(sender, user, request, **kwargs):
        form = RegistrationFormEx(data=request.POST)
        new_user = User.objects.get(username=request.POST['username'])
        digest=hmac.new(str(request.POST['username'])+str(request.POST['password1']), str(request.POST['password1']),hashlib.sha1).hexdigest()
        new_profile = UserProfile(user=new_user,api_key=digest)
        new_profile.save()
        #now add other fields including password hash as well

        uid = new_profile.id
        #Add username to session to pass it via Paypal later
        request.session['username']=request.POST['username']
        merchant_profile = MerchantProfile(user_id=uid,
            create_time=datetime.datetime.now(),
            modified_time=datetime.datetime.now(),
            payment_card_id=uid,
            current_state=1,
            name=request.POST['name'],
             )
        merchant_profile.save()

        return new_user

    user_registered.connect(user_created)

我的 Paypal 表单模板如下:

{% extends "base.html" %}
{% load i18n %}

{% block content %}
<p>
    {% trans "You are now registered. Activation email sent." %}

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="sumit_1349250468_per@sample.com">
    <input type="hidden" name="item_name" value="registration charge {{ request.session.username }}">
    <input type="hidden" name="item_number" value="1">
    <input type="hidden" name="amount" value="9.00">
    <input type="hidden" name="no_shipping" value="0">
    <input type="hidden" name="no_note" value="1">
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="lc" value="AU">
    <input type="hidden" name="bn" value="PP-BuyNowBF">
    <input type="image" src="https://www.paypal.com/en_AU/i/btn/btn_buynow_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online.">
    <img alt="" border="0" src="https://www.paypal.com/en_AU/i/scr/pixel.gif" width="1" height="1">
    <input type="hidden" name="return" value="http://url/payment_result?response=success">
    <input type="hidden" name="cancel_return" value="http://url/sorry">

</form>

</p>
{% endblock %}

在不更改任何视图的情况下,如何打印request.session.username此模板中的值?

4

1 回答 1

2

在 settings.py 中,TEMPLATE_CONTEXT_PROCESSORS应该包括django.core.context_processors.request- 这使request变量可用于您的模板。

虽然,警告一句:当用户激活他的电子邮件并在返回后登录时,会话变量可能已经改变。

于 2012-10-05T08:07:45.410 回答