Py学习  »  问与答

求救,做了一个搜索引擎,第一次搜索可用,但是刷新一下页面后就会出现python.exe停止工作的情况。

藤迅微博-weibo • 9 年前 • 5989 次点击  

现贴上view和html的代

`from django.http import HttpResponse

from django.shortcuts import render

import sys

def search_form(request):

    return render(request,'tweetsearch/search_form.html')

def search(request):

     sys.path.append(r"E:\Program Files\Python27")
    import jccIndri
    errors = []
    tweets = []
    if 'q' and 'field' in request.GET:
        q = request.GET['q']
        f = request.GET["field"]
        if not q:
            errors.append('Enter a search term.')
        elif not f:
            errors.append("place select a field")
        else:
            if f == "tweetid":
                tweets = jccIndri.query("#combine[tweetid]( %s )" % (q))
            elif f == "writer":
                tweets = jccIndri.query("#combine[writer]( %s )" % (q))
            else :
                tweets = jccIndri.query("#combine[fulltext]( %s )" % (q))
        if not errors:
            return render(request,'tweetsearch/search_result.html',
                    {'tweets': tweets, 'query': q,'place':f,})
        else:
            return render(request,'tweetsearch/search_form.html',{'errors': errors})
`

html代码如下 search_form.html

<head>
<title>Search</title>
</head>
<body>
<p>
<img src="{% static 'tweetsearch/tweet.jpg' %}" width="722" height="175">
</p>
<p>
{% if errors %}
    <ul>
        {% for error in errors %}
        <li>{{ error }}</li>
        {% endfor %}
    </ul>
{% endif %}

<form action="/tweetsearch/search/" method="get">
    <label>搜索域
    <select name='field' id='1'>
        <option value ="tweetid">tweetid</option>
        <option value ="writer">writer</option>
        <option value ="fulltext">fulltext</option>
    </select>
    </label>
    <input type="text" name="q">
    <input type="submit" value="Search">
</form>
</p>

</body> </html>

search.html如下所示:

<html>
<head>
    <title>result</title>
</head>
 <body>
    <p>You searched for: <strong>{{ query }}</strong> in field {{ place}}  </p>
    {% if tweets %}
        <ul>
            {% for tweet in tweets %}
            <li>{{ tweet.content }}</li>
            {% endfor %}
        </ul>
    {% else


    
 %}
        <p>No tweets matched your search criteria.</p>
    {% endif %}

</body>
 </html>

第一次检索时可以,刷新后,或者关掉检索结果页面,再次回到search_form页面,输入搜索词再检索就会出现python.exe已经停止工作。请问到底是怎么了。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/872
 
5989 次点击  
文章 [ 2 ]  |  最新文章 9 年前
我心荡漾
Reply   •   1 楼
我心荡漾    9 年前

同上,是个问题

爱情的枪
Reply   •   2 楼
爱情的枪    9 年前

可能是python出现异常,代码崩溃了

为啥 你在search里 每次都 sys.path.append(r"E:\Program Files\Python27")