weixin_33736832 2014-07-26 22:09 采纳率: 0%
浏览 32

Django客户端过滤

I'm pretty new to Django and I'm working on a site where I want the user to be able to filter out objects based on the objects' timestamp. I could easily do the filtering in views.py and reload the template, but how should I do this filtering client side?

Would it be a good idea to just load everything:

{% for obj in all_objects %}
    <p>{{obj.time}}</p>
{% endfor %}

and then use javascript to hide and show the relevant objects? I don't expect more than 1,000 objects.

I've taken a look at this: http://dustindavis.me/dynamic-filtered-drop-down-choice-fields-with-django.html and the author says that he would use AngularJS instead of a custom AJAX solution.

Is Angular my best choice or are there other frameworks that are easier to integrate with Django?

  • 写回答

1条回答 默认 最新

  • 撒拉嘿哟木头 2014-07-27 05:02
    关注

    You don't need a whole framework such as Angular to filter your results, though I'd certainly recommend using one of the popular MVC frameworks. However, if you go that route you will significantly change the architecture of your django application since Angular and such are really built for single-page applications, and would suit you best if your django project was exposing an API rather than rendering HTML.

    Having said that, to solve your problem, I would continue to use the same django template, and load the content of the list into a hidden HTML element. Then in your page's Javascript code, you can extract the relevant items from the list based on user selections. That code would be entirely Javascript, though.

    As a tip, i would use the django template to insert the filtering parameters as data attributes of your hidden HTML tags. An example:

    {% for obj in all_objects %}
        <p data-time='{{obj.time}}' data-somefilter='{{obj.somefiter}}'>{{obj.time}}</p>
    {% endfor %}
    

    This way you can easily use Jquery selectors to filter the data based on the requirements and then copy over the results to the visible HTML section where you display the filtered list

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试