简体   繁体   English

将HTML复选框中的前端数据传输到Forms.py/views.py/urls.py中的django

[英]Transferring front end data from HTML checkboxes to backend django in forms.py/views.py/urls.py

I have a bunch of checkboxes on my HTML page and want to store whether a checkbox was ticked or not in backend django. 我的HTML页面上有一堆复选框,并且想存储后端django中是否选中了该复选框。 My current HTML code is: 我当前的HTML代码是:

<input type="checkbox" name="activism" value="Yes">Activism & advocacy

I don't know how to modify my forms.py/urls.py/views.py to store whether a particular checkbox was ticked or not. 我不知道如何修改form.py/urls.py/views.py来存储是否选中了特定复选框。 Thank you very much. 非常感谢你。

There you are, it's not the best way to. 在那里,这不是最好的方法。 Since you are new to django, this will work. 由于您是django的新手,因此可以使用。 Alongside you're learning, take the django documentation as guide 在学习的同时,以django文档为指导

<form action='url' method='post'> {% csrf_token %}
   <input type="checkbox" name="activism" value="Yes">Activism & advocacy
   <input type='submit' value="submit" />
</form>

python view python视图

def viewName(request):
   if request.method == 'POST':
      # You have access to data inside request.POST
      activism = request.POST.get('activism')
      if activism:
           pass # Activism is checked

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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