アクセス禁止 403エラーが出てしまいます。
POSTMANを使いhttp://localhost:8000/app/index に、Keyにaccess_keyをValueに100を書いてPOSTを選択しSENDを送ると
<h1>アクセス禁止 <span>(403)</span> </h1> <p>CSRF検証に失敗したため、リクエストは中断されました。</p> <p>このメッセージが表示されている理由は、このサイトはフォーム送信時にCSRFクッキーを必須としているためです。このクッキーはセキュリティ上の理由(使用中のブラウザが第三者によってハイジャックされていないことを確認するため)で必要です。</p> <p>もしブラウザのクッキーを無効に設定しているならば、same-originリクエストのために少なくともこのサイトでは再度有効にしてください。</p>
と期待している動きではない動きをしました。
上記のURLに上記のKeyとValueを送ると、id_jsonが返ってくる仕組みにしたいです。
views.pyには、
from django.shortcuts import render from .forms import UserIDForm from .models import User import json def index(request): user_id_form = UserIDForm(request.POST or None) context = { 'user_id_form': user_id_form, } return render(request, 'index.html') def response(request): user_id_form = UserIDForm(request.POST or None) arr = [[100,2],[300,3],[500,4],[800,5],[200,6]] inp_id = 100 for i in range(int(len(arr))): if inp_id == arr[i][0]: find = True if find: uuid = arr[i][1] id_json = {"id": 100} print(id_json) else: print("Error") return render(request, 'response.html')
と書き、index.htmlには
<html lang="en"> <head> <meta charset="UTF-8"> <title>入力ページ</title> </head> <body> <form method="post" action="response"> {% csrf_token %} </form> </body> </html>
と書き、response.htmlには
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>応答</title> </head> <body> <h2>応答ページ</h2> </body> </html>
と書きました。
POSTMANでJSONを送るときhtmlでformタグを使うのが間違っているのでしょうか?どのように書けば自分の意図したシステムになりますか?
forms.pyには
from django import forms from .models import User class UserIDForm(forms.ModelForm): class Meta: model = User fields = ('id',) def __init__(self, *args, **kwargs): super(UserIDForm, self).__init__(*args, **kwargs) self.fields['id'].widget.attrs['class'] = 'form-control'
と
models.pyには
from django.db import models import uuid import random class User(models.Model): uu_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) id = models.IntegerField(null=True) regist_date = models.DateTimeField(auto_now=True) random_id = random.randint(1000, 9999)
と書きました。
回答2件
あなたの回答
tips
プレビュー