我试图在结果页面上的搜索框中显示用户的搜索查询。一个非常基本的问题,但是由于某种原因,它对我不起作用
我的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/