Django中的“CSRF Token Missing”- {% csrf_token %}已包含

mwyxok5s  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(86)

我一直在尝试做一个简单的登录页面,我的表单正在提交,然而,我不断得到csrf失败。我试着把标签放在几个地方,但都不起作用。
我已经在不同的地方包含了标签,用不同的帖子来改变小东西,但都不起作用。第一个是我的模板html,它加载。后者是view.py文件,它应该只是呈现一个视图。

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
    <title>Login Page</title>
  </head>
  <body>
    <h1>Login Page</h1>
    
    <form action="" method="post">
        {% csrf_token %}
        

        <div class="form-group">
            {% csrf_token %}
          <label for="exampleInputEmail1">Email address</label>
          <input style = "max-width: 50%;" type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
          <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
        </div>
        <div class="form-group">
            {% csrf_token %}
          <label for="exampleInputPassword1">Password</label>
          <input style = "max-width: 50%;" type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
        </div>
        
        <button href="/verify" style = "margin-top: 15px; margin-left: 25%; padding-right: 30px; padding-left: 30px;" type="submit" class="btn btn-primary">Submit</button>
      </form>
      <button style = "margin-top: 30px; margin-left: 25%; padding-right: 30px; padding-left: 30px;" type="submit" class="btn btn-primary">Sign Up</button>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  </body>
</html>

字符串

from django.shortcuts import render, redirect
from django.template import loader
from django.http import HttpResponse
# Create your views here.

def LogIn(request):
    
    template = loader.get_template('login.html')
    return HttpResponse(template.render())

def verify(request):
    pass
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
    <title>Login Page</title>
  </head>
  <body>
    <h1>Login Page</h1>
    
    <form action="" method="post">
        {% csrf_token %}
        

        <div class="form-group">
            {% csrf_token %}
          <label for="exampleInputEmail1">Email address</label>
          <input style = "max-width: 50%;" type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
          <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
        </div>
        <div class="form-group">
            {% csrf_token %}
          <label for="exampleInputPassword1">Password</label>
          <input style = "max-width: 50%;" type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
        </div>
        
        <button href="/verify" style = "margin-top: 15px; margin-left: 25%; padding-right: 30px; padding-left: 30px;" type="submit" class="btn btn-primary">Submit</button>
      </form>
      <button style = "margin-top: 30px; margin-left: 25%; padding-right: 30px; padding-left: 30px;" type="submit" class="btn btn-primary">Sign Up</button>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  </body>
</html>

的数据

nfzehxib

nfzehxib1#

尝试将 @csrf_protect 装饰器添加到视图函数中

from django.views.decorators.csrf import csrf_protect

@csrf_protect
def my_view(request):
   ...

字符串

相关问题