简体   繁体   English

CSRF 验证失败。 请求中止。失败的原因:CSRF 令牌丢失或不正确

[英]CSRF verification failed. Request aborted.Reason given for failure:CSRF token missing or incorrect

I am getting this error when I hit the submit button in the login form:当我点击登录表单中的提交按钮时出现此错误:

Forbidden (403) CSRF verification failed. Request aborted. CSRF token missing or incorrect.

setting.py设置.py

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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',
]

view.py视图.py

def contact_us(request):
    if request.method == "POST":
        con_name = request.POST['con_name']
        con_email = request.POST['con_email']
        con_company = request.POST['con_company']
        inquiry = request.POST['inquiry']
        con_message = request.POST['con_message']
        #context = RequestContext(request)
        #context_dict={'con_name':con_name}
        #context_dict.update(csrf(request))
        return render(request, 'contact_us.html', {'con_name':con_name})

    else:
        return render(request, 'contact_us.html',{})    

contact_us.html contact_us.html

 <form id="contact-form" action="{% url 'contact_us' %}" method="post"> {% csrf_token %} <div class="contact-form"> <div class="contact-input"> <div class="contact-inner"> <input name="con_name" type="text" placeholder="Name *"> </div> <div class="contact-inner"> <input name="con_email" type="email" placeholder="Email *"> </div> </div> <div class="contact-inner"> <input name="con_company" type="text" placeholder="Company"> </div> <div class="contact-select"> <div class="form-item contact-inner"> <span class="inquiry"> <select name="inquiry" class="select-item"> <option value="Your inquiry for">Your inquiry for</option> <option value="General Information Request">General Information Request</option> <option value="Partner Relations">Public Relations</option> <option value="Digital Marketing">Digital Marketing</option> <option value="Influencer Marketing">Influencer Marketing</option> <option value="Brand Creation">Brand Creation</option> <option value="Careers">Careers</option> <option value="Brand Creation">Web Development</option> <option value="Others">Others</option> </select> </span> </div> </div> <div class="contact-inner contact-message"> <textarea name="con_message" placeholder="Please describe what you need."></textarea> </div> <div class="submit-btn mt-20"> <button class="ht-btn ht-btn-md" type="submit">Send message</button> <p class="form-messege"></p> </div> </div> </form>

I am new to Django and most web development and I just cannot find the problem here.我是 Django 和大多数 web 开发的新手,我只是在这里找不到问题。 What am I missing?我错过了什么? How can I fix this issue?我该如何解决这个问题?

You need to pass RequestContext in render_to_response for csrf_token您需要在 render_to_response 中为 csrf_token 传递 RequestContext

For this: (views.py)为此:(views.py)

from django.template import RequestContext

...

return render_to_response('fileupload/upload.html', {'form': 

c['UploadFileForm']}, RequestContext(request)) c['UploadFileForm']}, RequestContext(request))

This passes the token for csrf to the template.这会将 csrf 的令牌传递给模板。

Visit this Link also, it would be helpful.也访问此链接,这会有所帮助。

I have faced the same problem.It got resolved when i put "{% csrf_token %}" before each input tags when they weren't together or separated by any other tags.我遇到了同样的问题。当我将“{% csrf_token %}”放在每个输入标签之前,当它们没有在一起或被任何其他标签分开时,它得到了解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM