推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
JUSTIGNOREME
V2EX  ›  Python

Django 视图函数应该向模板里传实例还是只传需要的属性呢?

  •  
  •   JUSTIGNOREME · Oct 27, 2018 · 1878 views
    This topic created in 2840 days ago, the information mentioned may be changed or developed.
    这两天在看 Django,有时候有点疑惑,以 Django 官方的教程为例:一个投票的应用,有问题和选项两个 model,问题在选项中有外键,可以通过问题获取到对应的选项,也可以通过选项获取到对应的问题,在渲染模板的时候是应该传对象的实例呢,还是只传用到的实例的某个或多个属性呢?还有就是如果模板用到了问题和选项这两个对象,是分开传还是只传其中一个,然后调用另外一个?
    1 replies    2018-10-27 20:44:39 +08:00
    ericls
        1
    ericls  
       Oct 27, 2018   ❤️ 2
    都可以的,只是传 instance 的情况下,注意不要在模板里面产生过多的数据库 query
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2621 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 15:43 · PVG 23:43 · LAX 08:43 · JFK 11:43
    ♥ Do have faith in what you're doing.