简体   繁体   中英

Django Submit Form and keep searchresults

I'm using Django for a page in which the user can search for images and add them to a list. On the top, there is a dropdown with all available lists, and in the pages "body" there is a search form where the user can search for images by tag.

<form action="{% url 'qwe:search' %}" method="get">
    {% csrf_token %}
    <input type="text" name="q" placeholder="Search..." {% if query_string %} value="{{query_string}}" {% endif %}>
    <input type="submit" value="Search">
</form>

On submit, the user gets the same page with a list of search results.

def search(request):
    query_string = request.GET["q"]
    if (query_string == None):
        return HttpResponseBadRequest()

    search_results_list = img_search(query_string, max_results=25)
    list_list = helpers.get_lists()

    context = {"search_results_list" : search_results_list, "query_string" : request.GET["q"], "lists " : list_list }
    return render(request, 'qwe/index.html', context)

I want the user to be able to create other lists, so I added a button next to the dropdown. When the user clicks on it, it opens a dialog (div) with a form containing an input for the new lists name.

The Problem: What is the best way, to submit the form for creating a new list, without losing the search results?

I do this mainly for learning purposes, so every hint is welcome.

Thank you.

The most obvious way that I can think of is to create an <input type="hidden" \\> as part of your form, whose value is the query_string . This would allow the server to know, when it receives the form details, what the original search query was.

In fact, assuming search_results_list is serializable (I would guess it is, but your code gives no idea as to what is in it), you could serialize it and send that string as a hidden input - that way you are transmitting the actual search results, rather than just the query that led to them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM