简体   繁体   English

如何在Django中的render()之后将参数传递给url?

[英]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. 我正在尝试使用Google's Custom Search Engine产品在个人Web应用程序页面上显示查询结果。

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. CSE的工作方式是将URL中的参数作为搜索词:即www.mydomain.com/results.html?q=hello+world将返回页面上“ hello world”查询的结果。 You put some JS code they give you on your page, so it's a bit of a black box. 您在页面上放置了一些它们给您的JS代码,因此有点黑框。

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 . 但是,在Django上使用URL routingrender()时,我猜测基础是www.mydomain.com/results被路由到views.results调用,该调用renders results.html views.results renders results.htmlwww.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? 通过表单提交查询并将其传递到www.mydomain.com/results.html?q=hello+world的最佳做法是什么,而不是重定向到www.mydomain.com/results并让Django呈现结果/ html文件?

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. 这与其他任何请求都没有什么不同:表单通过GET提交到结果视图,该视图从request.GET["q"]或任何地方获取搜索查询,进行搜索并将结果传递给模板。

Thanks for the responses, but I'm not sure that will work. 感谢您的答复,但我不确定是否可以。

First, the Google CSE JS code looks like this: 首先,Google CSE JS代码如下所示:

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

I'm not sure if it uses the request.GET["q"] method. 我不确定它是否使用request.GET [“ q”]方法。

Second, I want to process the "q" parameter with additional things beyond what the form sends. 其次,我想用表格发送之外的其他东西来处理“ q”参数。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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