gpt4 book ai didi

python - Django - 在结果页面的搜索框中显示查询

转载 作者:行者123 更新时间:2023-12-01 09:28:12 29 4
gpt4 key购买 nike

我正在尝试在结果页面的搜索框中显示用户的搜索查询。一个非常基本的问题,但由于某种原因,它对我不起作用

我的views.py看起来像:

def search(request):
result_list = []
if request.method == 'POST':
query = request.POST['query'].strip()
if query:
print(query)
print(type(query))
# Run our Webhose search function to get the results list!
result_list = run_query(query)
context_dict = {'result_list': result_list, 'query': query}
context_dict = {'result_list': result_list}
return render(request, 'rango/search.html', context_dict)

我的 search.html 模板如下:

{% block body_block %}
<div>
<h1>Search with Rango</h1>
<br/>
<form class="form-inline" id="user_form"
method="post" action="{% url 'search' %}">
{% csrf_token %}
<div class="form-group">
<input class="form-control" type="text" size="50"
name="query" value="{{query}}" id="query" />
</div>
<button class="btn btn-primary" type="submit" name="submit"
value="Search">Search</button>
</form>

<div>
{% if result_list %}
<h3>Results</h3>
<!-- Display search results in an ordered list -->
<div class="list-group">
{% for result in result_list %}
<div class="list-group-item">
<h4 class="list-group-item-heading">
<a href="{{ result.link }}">{{ result.title }}</a>
</h4>
<p class="list-group-item-text">{{ result.summary }}</p>
</div>
{% endfor %}
</div>
{% endif %}
</div>
</div>
{% endblock %}

有什么建议吗?

最佳答案

我的 context_dict 被覆盖。请找到以下解决方案:

def search(request):
query = ''
result_list = []
if request.method == 'POST':
query = request.POST['query'].strip()
if query:
print(query)
print(type(query))
# Run our Webhose search function to get the results list!
result_list = run_query(query)
context_dict = {'result_list': result_list, 'query': query}
return render(request, 'rango/search.html', context_dict)

关于python - Django - 在结果页面的搜索框中显示查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50189626/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com