Django POST form usage, djangopost form

Source: Internet
Author: User

Django POST form usage, djangopost form

The environment is as follows: django 1.7.8.

1. This error message will appear during the POST form.

Access prohibited (403) CSRF Verification Failed. corresponding interruption. helpReason given for failure: CSRF token missing or incorrect. in general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanic has not been used correctly.
For POST forms, you need to ensure: Your browser is accepting cookies. the view function passes a request to the template's render method. in the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL. if you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well
As those that accept the POST data. You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and
Only the initial error message will be displayed. You can customize this page using the CSRF_FAILURE_VIEW setting.

No. Check the above prompt to prevent CSRF, that is, security, and cross-site request forgery.

Follow the prompts above.{% Csrf_token %}, add the following in the from Form

The added html code is as follows.

{% extends "base.html" %}{% block title %}    hello{% endblock %}{% block content %}    <div class="container">      <form class="form-signin" action="/login_webmail/" method='post'>{% csrf_token %}        

Focus: {% csrf_token %} After from}

Follow the prompts on the official website and Baidu.

The code changes of views. py are as follows: Mainly return render_to_response('index.html ', context_instance = RequestContext (request ))

** Context_instance = RequestContext (request )**

 

from django.http import HttpResponseimport datetimefrom django.shortcuts import render_to_response#postfrom django.template import RequestContext#postdef webindex(request):    return render_to_response('index.html',context_instance=RequestContext(request))    

 

View method of received views

def login_webmail(request):    if 'inputUserName' in request.POST:        message = request.POST['inputUserName']    else:        message = "Not inputUserName"    return render_to_response('test_post.html',{'test_post_name':message})    

Test again. OK. Summary. There are only two steps.

1. Add {% csrf_token %} to the from form}

2. Add the from django. template import RequestContext import item to the view, and add context_instance = RequestContext (request) to the return result)

Then OK. It seems very simple. For new users, refer.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.