【问题标题】:django-open-id: CSRF verification faileddjango-open-id:CSRF 验证失败
【发布时间】:2011-07-22 19:22:39
【问题描述】:

我在这个 SO 问题中采纳了建议:What's the best solution for OpenID with Django? 并为我的应用程序安装了 django-openid-auth

但我就是无法正常工作,我在尝试登录时总是收到CSRF verification failed. Request aborted.

我已经检查了一切:

1.

{% csrf_token %} 存在于模板中:

<form name="fopenid" action="{{ action }}" method="post">
    {% csrf_token %}
    <fieldset>
        <legend>Sign In Using Your OpenID</legend>
        <div class="form-row">
            <label for="id_openid_identifier">OpenID:</label><br />
            {{ form.openid_identifier }}
        </div>
        <div class="submit-row ">
            <input name="bsignin" type="submit" value="Log in">
        </div>
 {% if next %}
        <input type="hidden" name="next" value="{{ next }}" />
 {% endif %}
    </fieldset>
</form>

2.

django_openid_auth 里面的views.py 中我发现,他们使用RequestContext

return render_to_response(template_name, {
                    'form': login_form,
                    redirect_field_name: redirect_to
                    }, context_instance=RequestContext(request))

3.

我的MIDDLEWARE_CLASSES 确实包含CsrfViewMiddleware

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

我就是不明白还有什么问题?你有什么想法吗?

我正在使用 Django 1.3 测试版。

更新

这似乎是我的全球性问题。我创建了一个简单的表单并得到了相同的结果:

def index(request):
    return render_to_response('index.html',
                              locals(),
                              context_instance=RequestContext(request))

index.html:

<form action="/" method="post">
    {% csrf_token %}
    <input type="text" name="test">
    <input type="submit">
</form>

呈现的 HTML:

<form action="/" method="post">
    <div style="display:none"><input type="hidden" name="csrfmiddlewaretoken" value="1fbd5345560d325bf05809260f7d43c3"></div>
    <input type="text" name="test">
    <input type="submit">
</form>

怎么了!?

【问题讨论】:

    标签: python django django-csrf django-openid-auth


    【解决方案1】:

    我尝试了“manage.py runserver localhost:8000”而不是“manage.py runserver”,它成功了 我的猜测是由于主机名中的 ip 它有一些 cookie 问题 顺便提一句。我在 django 1.3 的新副本中遇到了这个错误

    【讨论】:

      【解决方案2】:

      实际上,我认为这是我在 Django 中遇到的第一个错误。我已经将我的副本更新为 1.3 的新版本,一切都开始工作了。干得好,Django 开发人员!

      【讨论】:

        猜你喜欢
        • 2012-09-08
        • 2015-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-11
        相关资源
        最近更新 更多