简体   繁体   中英

How can I search variables from another template

I am in my homepage template where I have only search box like google main search page looks like, Now I want to search variables from another template and display it but nothing happen. Below is my code

Homepage template

<form action="{% url 'loststuffapp:IndexView' %}" method="GET" value="{{request.GET.q}}" class="navbar-form" role="search" style="margin-left: 25em;">
        <input type="text"placeholder="Search....." name="q">
        <button type="submit" onclick="/Miscellaneous"><i class="fa fa-search"></i></button>
</form>

Home page view

def IndexView(request):
    title="Homepage"
    return render(request, "loststuffapp/home.html", {"title":title})

Miscellaneous template

<div class="card-body">
  <p><label style="font-size:15px; font-weight: bold;color: black;">Jina la nyaraka:  </label>{{Doc.docs_name}}</p>
    <p><label style="font-size:15px; font-weight: bold;color: black;">Aina ya nyaraka:  </label>{{Doc.item_type}}</p>
    {% if Doc.image %}
 <div class="row">
    <div class="col-md-12">
       <img class="img-fluid" alt="Responsive image" src ="{{Doc.image.url}}" style="display: flex;" />
    </div>
 </div>

    {% endif %}
    <p>{{Doc.date}}</p>
</div>

Miscellaneous view

def Miscellaneous(request):
    query = request.GET.get('q', '')
    qsets=(Q(docs_name__icontains=query)|Q(item_type__icontains=query))
    return render(request, "loststuffapp/Miscellaneous.html", context={"documents":Documents.objects.filter(qsets)})

If I understand you correctly you want to pass the q parameter from the form on your home page to the Miscellaneous view. Currently your code just calls your IndexView in submitting the form and returns to it.

I guess you are expecting that the form to submit to the Miscellaneous view by adding the url to onclick of your button. I do not think that works like that as onclick expects some javascript function. Your form submits to the url you put into the action attribute of your form, and this is your homepage.

So the simplest approach would be to change the action attribute of your form to point to your Miscellaneous view:

<form action="{% url 'loststuffapp:Miscellaneous' %}" method="GET" value="{{request.GET.q}}" class="navbar-form" role="search" style="margin-left: 25em;">
        <input type="text"placeholder="Search....." name="q">
        <button type="submit"><i class="fa fa-search"></i></button>
</form>

Remark: I am not sure what you expect from the value attribute or your form

What you can do also is to return to your IndexView on submit of the form and redirect from there to the MiscellaneaousView , passing the query as an argument. However, this does not look necessary from what I read from your post.

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