Home  >  Article  >  Web Front-end  >  Detailed explanation of the use of Django controls and parameter passing

Detailed explanation of the use of Django controls and parameter passing

php中世界最好的语言
php中世界最好的语言Original
2018-04-19 16:48:513684browse

This time I will bring you djangoControl and detailed explanation of the use of passing parameters. What are the precautions for using Django controls and passing parameters? Here is the actual combat Let’s take a look at the case.

This article introduces the single selection and multi-selection in the djangoHTML form control, and explains how to pass parameters.

1. Form controls in HTML:

Forms are generally written in HTML as:

<form method="post" action=""> 这个method代表方法,方法一般有两个一个是'post',一个是'get',action是提交表单到何处,可填写一个网址。不填则默认到本页面。
{%csrf_token%} 这个是django中的一个标签,用于防止恶意攻击使用,如果不加入这个标签,会遇到不能提交的问题,处理麻烦一点,建议加上。
<input name="select" type="radio" value=&#39;radio&#39;>这就是一个单选标签,多选为type='checkbox'。 value是显示的内容,并且后端提交后也将此作为值,其中name是后端
获取时所用的如后端使用 select = request.POST['select']获取这个单选按钮的value,另外也可以用select = request.POST.get('select',None)来获取。
<input name="submit" type="submit" value="提交" />这就是一个提控件,其中的type='submit'会保证点击后表单(<form></form>)中的内容被提交到后端。
<input name="text" type="text" value="" />一个输入框
</form>
<!-表单结束>

The overall structure of the previous paragraph in HTML is

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
<form method="post" action=""> 
{%csrf_token%}
<input name="select" type="radio" value=&#39;radio&#39;>
<input name="text" type="text" value="" />
<input name="submit" type="submit" value="提交" />
</form>
</body>
</html>

Then the previous HTML is like this.

2. Accept data in django’s view.py:

The backend can write a def in the view to accept the data passed in by the frontend:

Such as:

def receive_data(request):
 if request.POST: # 如果数据提交
 print('有提交')
 
 select = request.POST.get('select',None)
 
 text = request.POST.get('text',None)
 print(select,text)
 return render(request,'your_html.html', locals()) # your_html.html改为你的html页面并且参考前面的博客建立url链接。

This is a simple case of django creation before and after interaction, conducted under django 1.10.5 python 3.5 html 5.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Echarts implements dynamic color-changing histogram

jquery implements all-select and inverse-select radio selection

jQuery operation background color gradient animation effect

The above is the detailed content of Detailed explanation of the use of Django controls and parameter passing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:HTML study notes 2Next article:HTML study notes 2