【问题标题】:How to create a search box in with a simple text field and a submit button in flask?如何在烧瓶中使用简单的文本字段和提交按钮创建搜索框?
【发布时间】:2013-09-18 07:37:33
【问题描述】:

按下提交时,表单应将值传递给服务器。但是我收到了这个 500 内部服务器错误。这是我的views.py代码:-

from app import app
from flask import render_template,request
import feedparser
import json
@app.route('/')
@app.route('/index')
def search():

    return render_template('index.html')

@app.route('/searchRSS',methods=['POST'])
def search_results():
    feed = feedparser.parse("http://news.google.com/news?hl=en&gl=in&q="+request.form['query']+"&um=1&output=rss" )
    posts = []
    for i in range(0,len(feed['entries'])):
        posts.append({
            'date': feed['entries'][i].title,
            'title': feed['entries'][i].updated,
            'description': feed['entries'][i].description

        })
    return json.dumps(posts, separators=(',', ':'))

这是 index.html :-

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
    window.onload=function (){
    $("#searcher").submit(function(ev) {

    /* stop form from submitting normally */

        ev.preventDefault();



        $.post("/searchRSS", $("#searcher").serialize(),function(o)    {document.getElementById("result").innerHTML=o;});
})};
</script>
<form id="searcher" method="post" action="#">
<input type="text" id="query" name="query" required/>
<input type="submit" value="Get Feed"/>
</form>
<div id="result"></div>

【问题讨论】:

  • 你打开调试模式可能会看到确切的错误吗? app.debug = True
  • 是的,我有,但我不知道如何使用它
  • 如果你有调试,你能发布整个错误堆栈跟踪吗?
  • POST 127.0.0.1:5000/searchRSS 500 (内部服务器错误) jquery.js:9597
  • @codegeek 我没有得到任何回溯实际上就像 * 在 127.0.0.1:5000 上运行 * 使用重新加载器重新启动你能告诉我该怎么做吗?

标签: python ajax jquery flask


【解决方案1】:

Chardet 2.1.3 似乎没有移植到 Python 3。您可以阅读作者关于移植 chardet here 的案例研究。如果您查看 PyPi 发行版的源代码,它与案例研究中的 Python 3 端口不同。

我能够在 GitHub 上找到一个移植到 Python 3 的 chardet 分支:https://github.com/byroot/chardet。您可能希望使用该 fork 对其进行测试,看看它是否可以解决问题。

编辑: 你应该可以使用 pip 安装:pip install https://github.com/byroot/chardet/zipball/master 你可能想先删除当前的 chardet 或者在它自己的 virtualenv 中测试它。

【讨论】:

  • 谢谢。我删除了 python 3 并安装了 python 2.7,我现在可以导入 chardet。但我仍然遇到这个 500 内部服务器问题。请帮我解决它
猜你喜欢
  • 2013-01-13
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多