我试图通过在下拉列表中使用更改功能来使用ajax从数据库获取查询,因此页面不需要重新加载。我一直收到错误“ValueError:View函数没有返回响应”我改变了我在django上使用ajax的搜索功能所使用的代码就像这样,但现在我没有看到我出错的地方。这就是我的代码。下拉菜单是Flask-WTForm选择字段。我不再收到回溯错误,但我仍然没有返回任何数据。
ajax.js
var csrftoken = $('meta[name=csrf-token]').attr('content')
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken)
}
}
})
$(function() {
$('#client').change(function() {
$.ajax({
type: "POST",
url: "/eventselect",
data: {
'eventview': $('#client').val(),
},
success: eventSuccess,
dataType: 'html'
});
});
});
function eventSuccess(data, textStatus, jqXHR)
{
$('#eventslog').html(data);
}
views.py
@app.route('/', methods=['GET', 'POST'])
# @login_required
def home():
viewform = EventViewForm()
return render_template('home.html', viewform=viewform)
@app.route('/eventselect', methods=['GET', 'POST'])
def eventselect():
viewform = EventViewForm()
today = datetime.today()
posted1 = '%i-%i-%i 00:00:00' % (today.year, today.month, today.day)
posted2 = '%i-%i-%i 23:59:59' % (today.year, today.month, today.day)
if viewform.validate_on_submit():
eventview = viewform.data
print(eventview)
if eventview is not None and eventview !=u"":
eventview = viewform.data
if eventview == 'client1':
eventview = EventsClient1
elif eventview == 'client2':
eventview = EventsClient2
else:
eventview = EventsClient3
eventviewlogs = eventview.query.filter(eventview.posted.between(posted1, posted2))
else:
eventviewlogs = []
return render_template('eventselect.html', eventviewlogs=eventviewlogs)
eventselect.html
{% if eventviewlogs.count() > 0 %}
{% for ev in eventviewlogs %}
{{ ev.posted }} | {{ ev.poster }} | {{ ev.event }}
{% endfor %}
{% elif eventviewlogs.count() = 0 %}
No items found
{% endif %}
home.html的
{% extends "base.html" %}
{% block content %}
{{ viewform.client() }}
<div id="eventslog"></div>
{% endblock content %}
回溯
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1478, in full_dispatch_request
response = self.make_response(rv)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1566, in make_response
raise ValueError('View function did not return a response')
ValueError: View function did not return a response
答案 0 :(得分:0)
通过修复我对eventselect的调用来解决问题。我没有得到request.data
@app.route('/eventselect/', methods=['GET', 'POST'])
def eventselect():
today = datetime.today()
posted1 = '%i-%i-%i 00:00:00' % (today.year, today.month, today.day)
posted2 = '%i-%i-%i 23:59:59' % (today.year, today.month, today.day)
if request.method == "POST":
eventview = request.data
print(eventview)
# if eventview is not None and eventview !=u"":
# eventview = viewform.data
if eventview == 'EventsClient1':
eventview = EventsClient1
elif eventview == 'EventsClient2':
eventview = EventsClient2
else:
eventview = EventsClient3
print(eventview)
eventviewlogs = eventview.query.filter(eventview.posted.between(posted1, posted2))
else:
eventviewlogs = []
return render_template('eventselect.html', eventviewlogs=eventviewlogs)