-1

views.py ファイルに次のようなビューがあります。

def index(request):
    c = {}
    c.update(csrf(request))
    if request.method == 'POST':
        form = MyRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/register_success')

    args = {}
    args.update(csrf(request))

    args['form'] = MyRegistrationForm()

    return render_to_response('index.html', c), render_to_response('index.html', args)

私のindex.htmlは次のようになります。

    <form action="/accounts/auth/" method="post">{% csrf_token %}
        <label for="username">User name:</label>
        <input type="text" name="username" value="" id="username">
        <label for="password">Password:</label>
        <input type="password" name="password" value="" id="password">

        <input type="submit" value="login" />

    </form>
    <h2>Register</h2>
    <form action="/accounts/register/" method="post">{% csrf_token %}
        {{form}}

        <input type="submit" value="Register" />

    </form>

action="/accounts/auth/" のフォームの c をレンダリングし、action="/accounts/register/" のフォームの args をレンダリングしたい..どうすればそうすることができるでしょうか?

4

1 に答える 1

1

2 つの個別の csrf トークンは必要ありません。辞書cにはトークン以外のデータが含まれていないため、ユーザーrender_to_response('index.html', args)は目的を達成できます。

またはさらに良いことに、を使用しますrender(request, 'index.html', args)here で説明されているように、テンプレート コンテキスト プロセッサを使用して、ビューで csrf トークンを取得することもお勧めします。

于 2013-07-28T18:39:59.970 に答える