django CSRF验证仅在移动设备上失败

时间:2015-04-13 18:08:53

标签: django python-2.7 mobile csrf

我有一个可以从桌面浏览器中完美运行的登录表单。但是,当我尝试从Android手机登录时,收到以下消息:

"禁止(403) CSRF验证失败。请求中止。 失败的原因: 未设置CSRF cookie

希望有人可以帮我解决这个问题。不知道为什么它可以在桌面上工作但不能移动..

-Django版本:Django == 1.6.5

MiddleWare课程

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

views.py

from .forms import SubscriberForm
from .newforms import SignInForm, ForgotPassForm, ResetPassForm
from .models import Subscriber, Sock

from django.shortcuts import render, render_to_response, RequestContext,        HttpResponseRedirect, get_object_or_404
from django.contrib.auth.models import User
from django import forms
from django.core.mail import send_mail
from django.contrib import messages
from django.contrib.auth import authenticate, logout, login
from django.core.context_processors import csrf

import stripe, time, datetime, json, mailchimp
from dateutil.relativedelta import *
from datetime import *
from mailchimp import utils

def signin(request):

    form = SignInForm(request.POST or None)
    title = 'Sign In'
    context = {
        'form':form,
        'title':title,
        }

    if request.POST:
        if form.is_valid():
            username = form.cleaned_data['email']
            username = username.lower()
            password = form.cleaned_data['password']

            user = authenticate(username=username, password=password)

            if user is not None:
                if user.is_active:
                    login(request, user)
                    pk = request.user.id
                    u = User.objects.get(pk = pk)
                    try:
                        o = Subscriber.objects.get(customer = u)
                        if o.stripe_id:
                            return HttpResponseRedirect('/home')
                        else:
                            messages.success(request,"let us know your style.")
                            return HttpResponseRedirect('/first')
                    except:
                        messages.success(request,"let us know your style.")
                        return HttpResponseRedirect('/first')
                else:
                    #user exists but account has been disabled
                    user.is_active = True
                    user.save()

                    return HttpResponseRedirect('/shippinginfo')
            else:
                #credentials are wrong or user does not exist
                messages.success(request, "we do not recognize that user/pass combo")

                return HttpResponseRedirect('/signin')

        args = {}
        args['form'] = form
        context['args'] = args

    return render(request, 'signin.html', context)

HTML

{% block signup %}

<section id="contact">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h2>Sign In</h2>
                <hr class="colored">
                <p>Welcome Back</p>
            </div>
        </div>

{% if form.errors %}
<div class='col-sm-10 col-sm-offset-1'>
{% for error in form.non_field_errors %}
    <div style="text-align: center;">
{% if 'username' in error %}
    <strong>{{ error|escape }}</strong>
    <br />
    <a href="/signup">Create An Account</a>
    <br />
    <a href="/forgotpass">Forgot Password</a>
{% else %}
    <strong>{{ error|escape }}</strong>
{% endif %}
</div>
{% endfor %}
{% endif %}

{% if messages %}

    <div class='row'>
        <div class='col-sm-6 col-sm-offset-3'>

{% for message in messages %}

            <p{% if message.tags == "success" %} 
            {% else %} style="color:#000;"
            {% endif %}><b>{{ message }}</b></p>

{% endfor %}

<br />
<a href="/signup">Create An Account</a>
<br />
<a href="/forgotpass">Forgot Password</a>
</div>
</div>
</div>

{% endif %}


</div>

    <div class="row content-row">
        <div class="col-lg-8 col-lg-offset-2">
            <form name="sentMessage" id="contactForm" method="POST" novalidate>
{% csrf_token %}
                <div class="row control-group">
                    <div class="form-group col-md-8 col-sm-12 col-xs-12 col-md-offset-2 col-sm-offset-0 xs-offset-0 floating-label-form-group controls">
                        <label>Email Address</label>
                        {{ form.email }}
                        <p class="help-block text-danger"></p>
                    </div>
                </div>

                <div class="row control-group">
                    <div class="form-group col-md-8 col-sm-12 col-xs-12 col-md-offset-2 col-sm-offset-0 xs-offset-0 floating-label-form-group controls">
                        <label>Password</label>
                        {{ form.password }}
                        <p class="help-block text-danger"></p>
                    </div>
                </div>

                <br />
                 <div id="success"></div>
                    <div class="row">
                        <div class="form-group col-md-8 col-sm-12 col-xs-12 col-md-offset-2 col-sm-offset-0 xs-offset-0">
                            <button type="submit" class="btn btn-outline-dark">Sign Me In</button>
                        </div>
                    </div>
            </form>
        </div>
    </div>
    </div>
    </section>
{% endblock %}

0 个答案:

没有答案