简体   繁体   English

带有模板的Django动态页面生成

[英]Django dynamic page generation with templates

I want to generate a page using templates and content blocks. 我想使用模板和内容块生成页面。 The homepage generates a bunch of checkboxes to generate a searchquery for some specific values. 主页会生成一堆复选框,以生成一些特定值的搜索查询。

<div id="searchbar">
  {% for foo in bar %}
    <input type="checkbox" name="foovar" value={{foo.name}}{%if foo.name in p %}checked=checked{%endif%}>{{foo.name}}<br>
  {%endfor%}
  <input type="submit" value="Submit">
</div>
<div id="searchresult">
{% block content %}
{% endblock %}

The value 'bar' is the parameter passed to the template containing all specific values: 值“ bar”是传递到模板的参数,包含所有特定值:

 return render_to_response('testlink/foobar.html',{'bar':strings_from_database})

Now, upon submitting the values, they are passed to my view, some magic happens, and the result is passed via 现在,在提交值后,它们将传递到我的视图,发生了一些魔术,结果通过

 return render(request,'extend/extend.html',{'result':result,'p':queried_values})

result is the result, p contains the queried values (to keep the checkboxes checked after submitting). 结果是结果,p包含查询的值(在提交后保持复选框处于选中状态)。

 {% extends "testlink/foobar.html" %}
 {% block content %}
 <b>results:</b>  <br>
 {% for result in result %}
     {{result.someattribute}}<br>
 {% endfor %}
 {% endblock %}

Now, my problem: The checkboxes disappear, probably as the 'for foo in bar' loop is executed again. 现在,我的问题是:复选框消失了,可能是因为再次执行了“ for foo in bar”循环。 Is there a way to prevent that from happening, without to hardcode the checkboxes into my template? 有没有一种方法可以避免这种情况发生,而无需将复选框硬编码到我的模板中? This would work (i did it with a few checkboxes, but doing this with too many searchvalues is no fun. Also i would like to avoid additional database hits and passing the parameters again. 这可以工作(我只用了几个复选框就做到了,但是用太多的searchvalues这样做是没有意思的。另外,我想避免额外的数据库命中并再次传递参数。

I agree with the comments above, using forms is almost always a better idea. 我同意以上评论,使用表格几乎总是一个更好的主意。 But in response to your problem, if you are not passing the bar variable to the template, the loop that parses the checkboxes will not be executed. 但是针对您的问题,如果没有将bar变量传递给模板,则不会执行解析复选框的循环。 You would need to add bar to the context for extend.html : 您需要将bar添加到上下文中的extend.html

return render(request,'extend/extend.html',
    {'result':result,'p':queried_values, 'bar':strings_from_database})

Are you not doing this just to prevent hitting the DB twice? 您不是为了防止两次访问数据库而这样做吗? Has it proven a very expensive query to run? 它是否已证明是运行起来非常昂贵的查询?

Apart from setting up caching, you can always pass ALL the checkboxes "names" along with the form. 除了设置缓存之外,您始终可以将所有复选框“名称”与表单一起传递。 You can add hidden inputs, like so: 您可以添加隐藏的输入,如下所示:

{% for foo in bar %}
    <input type="checkbox" name="foovar" value="{{foo.name}}" {%if foo.name in p %}checked="checked"{%endif%}>{{foo.name}}<br>
    <input type="hidden" name="foovar_all" value="{{foo.name}}" />
{%endfor%}

Then you need to collect the values with something like: 然后,您需要使用以下方式收集值:

bar = request.POST.getlist('foovar_all')

But you would need to rethink your code so the bar variables hold just the names of those objects in both views, it looks like it is a list of objects currently. 但是您需要重新考虑代码,以便bar变量仅在两个视图中都包含那些对象的名称,看起来它是当前的对象列表。 Again, is it really necessary to avoid that query? 同样,真的有必要避免该查询吗?

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

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