【问题标题】:csrf(forbidden 403) error in djangodjango中的csrf(禁止403)错误
【发布时间】:2013-05-25 03:44:26
【问题描述】:

这段代码的第二个函数有什么问题?我们在第一个函数中使用相同的语法,它可以正常工作,但第二个没有,我们有 csrf 错误...(禁止 403)

from User.models import User_account
from django.http import HttpResponse
from django.core.context_processors import csrf
from django.shortcuts import render_to_response, render

def get_info(request):

    if request.method == 'POST':
        if request.POST['passwd']!=request.POST['cpasswd']:
            return render(request,'signup.html',{'error':'The confirmed password does     not match the first one'})
        new_user = User_account.objects.create(first_name=request.POST['first_name'],last_name=request.POST['last_name'],e_mail=request.POST['e_mail'],passwd=request.POST['passwd'])
    new_user.save()
    return HttpResponse("your account was built")


def signin(request):
    if request.method != 'POST':
        return render(request, 'login.html', {'error': 'Email or Password is incorrect'})

    user_profile = User_account.objects.filter(e_mail = request.POST['user_email'], passwd = request.POST['password'])     

    if user_profile:
        return render(request, 'login.html', {'error': 'Ecorrect'})
    else:
        return render(request, 'login.html', {'error': 'Email or Password is incorrect'})

【问题讨论】:

  • 模板中有csrf标签吗?
  • 是的,我们拥有它并将其用于第一个功能,它可以工作!
  • 您是否使用 AJAX 发送表单?
  • 不! -------------------------------------------------- ----
  • 你能分享你的模板吗?第一个函数返回到 signup.html,第二个函数返回到 login.html(为了清楚起见,我想我们需要看看它们里面有什么)

标签: django csrf


【解决方案1】:

试试这个:

def signin(request):
    if request.method != 'POST':
        return  render_to_response('login.html', {'error': 'Email or Password is incorrect'}, context_instance=RequestContext(request))
        user_profile = User_account.objects.filter(e_mail = request.POST['user_email'], passwd = request.POST['password'])     

    if user_profile:
        return render_to_response('login.html', {'error': 'Ecorrect'}, context_instance=RequestContext(request))
    else:
        return render_to_response("perfil.html",  {'error': 'Email or Password is incorrect'}, context_instance=RequestContext(request))

【讨论】:

  • 为什么?有人可以帮我解决吗?
猜你喜欢
  • 1970-01-01
  • 2018-11-06
  • 2018-01-14
  • 2016-05-08
  • 2018-02-28
  • 1970-01-01
  • 2014-11-15
  • 2019-03-02
  • 2021-07-08
相关资源
最近更新 更多