如何在django缓存中保存类结果并在其他类中使用它 [英] How to save class results in django cache and use it in other class
本文介绍了如何在django缓存中保存类结果并在其他类中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HTML搜索文件
<div class="content-section">
<h1 class="mb-3">{{ user.username }}</h1>
<form method="GET" action="{% url 'doctor:search' %}">
<input name ="q" value="{{request.GET.q}}" placeholder="search..">
<button class="btn btn-success" type="submit">
Search
</button>
</form>
</div>
VIEWS.py
我喜欢将查询"值保存在缓存中,以后在不同的views.py类中使用
VIEWS.py
i like to save 'query' value in cache and use it later in different views.py class
class SearchResultsView(ListView):
model = User
template_name = 'all_users/doctor/search.html'
def get_queryset(self): # new
*query = self.request.GET.get('q')*
object_list = User.objects.filter(Q(username__icontains=query))
return object_list
推荐答案
您可以使用 render_to_response
返回多个这样的对象:
You can use render_to_response
for returning multiple objects like that:
def get_queryset(self): # new
query = self.request.GET.get('q')
object_list = User.objects.filter(Q(username__icontains=query))
another_object_list = Doctor.objects.filter(Q(username__icontains=query))
context = {
'object_list': object_list,
'another_object_list': another_object_list
}
return render_to_response('search.html', context)
您可以在模板中调用此上下文:
And you can call this context in your template:
{{object_list}}{{another_object_list}}
{{ object_list }} {{ another_object_list }}
这篇关于如何在django缓存中保存类结果并在其他类中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文