简体   繁体   中英

How to pass parameters into url after render() in Django?

I'm trying to use Google's Custom Search Engine product to display results from a query on a page for a personal web app.

How CSE is working is that it takes parameters from the URL as search terms: ie, www.mydomain.com/results.html?q=hello+world would return results for a "hello world" query on the page. You put some JS code they give you on your page, so it's a bit of a black box.

However, with URL routing and render() on Django , I'm guessing the basics is that www.mydomain.com/results gets routed to a views.results calls which renders results.html as www.mydomain.com/results .

What is the best-practice for submitting a query through a form, and passing it to www.mydomain.com/results.html?q=hello+world instead of redirecting to www.mydomain.com/results and having Django render the results/html file?

Sorry, I'm relatively new. I can try to piece things together, but I feel there has got to be a very efficient way of handling this situation. Thanks for understanding

You're completely overthinking this. There's nothing different here from any other request: the form submits via GET to the results view, the view gets the search query from request.GET["q"] or wherever, does the search and passes the results to the template.

Thanks for the responses, but I'm not sure that will work.

First, the Google CSE JS code looks like this:

  <div class="container">
     <gcse:searchresults-only></gcse:searchresults-only>
  </div>

I'm not sure if it uses the request.GET["q"] method.

Second, I want to process the "q" parameter with additional things beyond what the form sends. Does that make sense?

I want the workflow to be like this:

    query = {'keywords':request.GET.get('k'),
         'urls':request.GET.get('u'),
         'collections':request.GET.getlist('c'),
         'filetypes':request.GET.getlist('f')
         }
    query_final = create_query(query) # Some other function
    return render(request,'app_search/results.html') # Need to go to domain.com/results.html?q=blahblahblah

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