【发布时间】:2016-05-09 01:10:00
【问题描述】:
我正在为一个 Flask 应用程序而苦苦挣扎,该应用程序在提交表单时给了我错误的请求。我已经阅读了 Flask 的一些文档和一些 SO 页面。我确定我缺少一些简单的东西。
简而言之,我开发了一个模板来接收名为“placeindex.html”的表单。表单“名称”数据全部匹配。如果我将“placeindex.html”的名称更改为“index.html”,即使我指向“placeindex.html”文件,它也可以正常工作。下面的代码(查看):
@app.route('/add_place', methods=['GET', 'POST'])
def add_place():
username = session['username']
placename = request.form['place']
address = request.form['address']
city = request.form['city']
state = request.form['state']
zipcode = request.form['zipcode']
alias = request.form['alias']
searchword = request.args.get('key', '')
print(searchword)
Place(session['username']).new_place(placename, address, city, state, zipcode, alias, username)
return render_template('placeindex.html')
placeindex.html:
{% extends "layout.html" %}
{% block place %}
<h2>Home</h2>
{% if session.username %}
<h3>Add new 'placeindex'</h3>
<form action="{{ url_for('add_place') }}" method="post">
<dl>
<dt>Name:</dt>
<dd><input type="text" size="30" name="place"></dd>
<dt>Address:</dt>
<dd><input type="text" size="30" name="address"></dd>
<dt>City:</dt>
<dd><input type="text" size="30" name="city"></dd>
<dt>State:</dt>
<dd><input type="text" size="2" name="state"></dd>
<dt>Zip Code:</dt>
<dd><input type="text" size="10" name="zipcode"></dd>
<dt>Nickname:</dt>
<dd><input type="text" size="30" name="alias"></dd>
</dl>
<input type="submit" value="Save">
</form>
{% endif %}
<br>
<h3>My Places</h3>
{% include "display_posts.html" %}
{% endblock %}
我已经去掉了所有的验证代码来尝试解决这个问题。
layout.html(如果有帮助的话):
<!doctype html>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
<div class="page">
<div class="metanav">
{% if session.username %}
Logged in as {{ session.username }}
{% endif %}
<a href="{{ url_for('index') }}">Home</a>
{% if not session.username %}
<a href="{{ url_for('register') }}">Register</a>
<a href="{{ url_for('login') }}">Login</a>
{% else %}
<a href="{{ url_for('profile', username=session.username) }}">Profile</a>
<a href="{{ url_for('logout') }}">Logout</a>
<a href="{{ url_for('add_place') }}">Places</a>
<a href="{{ url_for('add_trip') }}">Trips</a>
<a href="{{ url_for('add_delegate') }}">Delegates</a>
{% endif %}
</div>
{% for message in get_flashed_messages() %}
<div class="flash">{{ message }}</div>
{% endfor %}
{% block body %}{% endblock %}
{% block post %}{% endblock %}
{% block place %}{% endblock %}
{% block trip %}{% endblock %}
</div>
一旦我打开文件的“index.html”版本并发送表单数据,它就会刷新到正确的文件,但是如果没有 BAD REQUEST 页面,我就无法直接去那里。
谢谢
【问题讨论】: