How to get data from a form in Python using Flask
本问题已经有最佳答案,请猛点这里访问。
这是我的runn.py代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | @app.route('/', methods=['GET', 'POST']) def index(): with app.open_resource('static/docs/student1.txt') as f1: content1 = f1.read() with app.open_resource('static/docs/student2.txt') as f2: content2 = f2.read() with app.open_resource('static/docs/student3.txt') as f3: content3 = f3.read() with app.open_resource('static/docs/student4.txt') as f4: content4 = f4.read() contents = [content1, content2, content3, content4] students = ['Student1','Student2','Student3','Student4'] selected_student = requests.GET['student_form'] if (selected_student == 'Student2'): content_selected = content2 elif (selected_student == 'Student3'): content_selected = content3 elif (selected_student == 'Student4'): content_selected = content4 else: content_selected = content1 return render_template('index.html', students = students, content_selected=content_selected) |
这是我的HTML代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <form action="runn" name='frm' method='POST'> Which Student <select name="student_form" method="GET" action="/"> <option value="{{students[0]}}" selected>{{students[0]}}</option> {% for student in students[1:] %} <option value="{{student}}">{{student}}</option> {% endfor %} </select> <input type='submit' value='submit'> </form> [cc lang="python"] {{ content_selected }} |
号
1 2 3 4 5 | <P>我无法在视图中显示表单数据。错误是:</P><blockquote> <p> AttributeError: 'module' object has no attribute 'GET' </p> </blockquote>号<hr><P>你能试试这个吗?</P>[cc lang="python"]selected_student = request.form.get('student_form') |
烧瓶的
请参阅下面的示例,了解如何编写视图函数…
查看功能
1 2 3 4 5 6 7 8 9 | @app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': student = request.forms.get('student') print(student) with app.open_resource('static/docs/{}.txt'.format(student)) as fi: content = fi.read() return render_template('base.html', content=content) return render_template('base.html') |
HTML
1 2 3 4 5 6 7 8 9 | <form method="POST" action="."> <select name="student"> <option value="student1">One</option> <option value="student2">Two</option> </select> <input type="submit" name="submit" value="Submit"> </form> {{ content }} |
号
- 检查他的python代码片段。
- 谢谢@raja simon@pissal,这很管用。但也有一些错误。当我单击提交按钮时,1
2
3
4
5
6
7{{ content_selected }}</pre> </div></wyn>不起作用。</li><li>对不起的。。。<wyn>selected_student = request.args.get('student_form')</wyn>是个工程。</li><li>@苏莱曼杜兹根不,那不对。我还有一个问题,为什么在你的选择领域,你有行动和方法?</li><li>@RajaSimon,首先我没有使用表单,所以我在选择的部分添加了get和action,然后我创建了一个表单,我忘记了删除它们。</li><li>@Raja如果我使用<wyn>selected_student = request.forms.get('student_form')</wyn>,就会出现这样的错误:<wyn>AttributeError: 'Request' object has no attribute 'forms'</wyn></li><li>你又犯了错误。是<wyn>form</wyn>,不是<wyn>forms</wyn>。做这件事</li></ul>[/collapse]</div><p><center>[wp_ad_camp_1]</center></p><hr><P>我想你可以使用<wyn>request</wyn>模块。</P>[cc lang="python"]@api.route('/resource', methods=['GET', 'POST'])
def ticket(*args, **kwargs):
if(request.method == 'POST'):
return ResourceController.create(request)
elif(request.method == 'GET'):
return ResourceController.index()。
在控制器内部
1
2
3
4def create(request):
resource_type = request.form['resource_type']
...
...