Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

How to save class results in django cache and use it in other class

Html search file

    <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 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 }}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...