首页 > 解决方案 > Django 从 html 表单中检索数据

问题描述

这是我的 html 表单

<form action="#about" method="POST">
 {%csrf_token%}
<h2>Signup today</h2>
<input type="text" name="name"  class="form-control" placeholder="Full name" required="">

<input type="email" name="email" class="form-control" placeholder="Your email address" required="">

<input type="password" name="password" class="form-control" placeholder="Your password" required="">

<button class="submit-btn form-control" id="form-submit">Get started</button>
</form>

这是views.py

def Student(request):
    if request.method=="POST":
        print('this post')
        name =request.POST['name']
        email =request.POST['email']
        print(name, email)  
    return render(request,'#about') 

这是我的 urls.py

from django.urls import path

from . import views

urlpatterns = [
      path('',views.index, name='index.html'),  
      path('',views.Student, name="#about"),  
]

我是 Django 的新手,我在过去的 24 小时里试图做到这一点,它没有显示任何错误,但它没有打印 views.py 中的任何内容,也没有检索表单数据,谁能向我解释我做错了什么以及如何正确地将html数据表单检索到django?非常感谢您!

标签: pythondjango

解决方案


你的按钮需要type="submit" 这样:

<button class="submit-btn form-control" id="form-submit" type="submit">Get started</button>

推荐阅读