如何在 Django 视图请求帖子上更新模板



我正在做一些过滤。我的页面上有初步结果,然后有一个表单,在选择时,我想过滤一些数据并更新模板。

不过,模板似乎没有更新。

from .services import service
# Home Page View
def home_view(request, *args, **kwargs):
update_list = service.get_updates()
if request.method == 'POST':
filter_string = request.POST.get('filter')
update_list = [update for update in update_list if update['type'] == filter_string]
print(len(update_list))
page = request.GET.get('page', 1)
paginator = Paginator(update_list, 2)
try:
updates = paginator.page(page)
except PageNotAnInteger:
updates = paginator.page(1)
except EmptyPage:
updates = paginator.page(paginator.num_pages)
context = {
'updates': updates
}
return render(request, "home.html", context)

模板文件:

<form method="post" id="filterformhome">
{% csrf_token %}
<select class="frm-input" onchange="homeFilter(this);">
<option value="job">{% trans "Jobs" %}</option>
<option value="market">{% trans "Products" %}</option>
<option value="question">{% trans "Questions" %}</option>
<option value="blog">{% trans "Blogs" %}</option>
</select>
</form>
{% for update in updates %}
{{update.title}}
{% endfor %}

这样做的更好方法是什么?

为此,您需要使用 JS,否则您只会看到通过刷新页面反映的更改。

最新更新