简体   繁体   中英

Forbidden 403 CSRF error when using request.post.get() method in views.py from html page

This is the views.py code

from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpRequest
from django.db import models
from datetime import datetime
# Create your views here.

def postdata(request):
    if request.method == "POST":
        data=request.POST.get('data')
return HttpResponse(data)`

this is the html code

<form action="http://127.0.0.1:8000/minor/postdata/" method="POST">
data:<br>
  <input type="text" name="data"><br>


<input type="submit" value="Submit">

</form>

i cant find the solution for request with post data from simple html page i can get the code working for GET requests. i need to connect this server from android app

It is because you are not sending the csrf_token with your post request. For security reasons, django checks csrf_tokens in all POST requests.

You can add csrf_token in your template using csrf_token templatetag, like this :

<form action="http://127.0.0.1:8000/minor/postdata/" method="POST">
{% csrf_token %}

Read: How to use CSRF token in Django

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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