V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
pythonee
V2EX  ›  Python

[django]怎么实现字段的联动呢

  •  
  •   pythonee · 2015-04-12 22:56:32 +08:00 · 2749 次点击
    这是一个创建于 3321 天前的主题,其中的信息可能已经有所发展或是发生改变。
    假设存在以下model

    class model():
    type = charField(chioics={"按里程计价","按时间计价"})
    distance = integerField()
    during = decimalField()


    有两种计价方式,type显示为下拉框,现在想根据下拉框不同的值,也即是不同的计价方式,填写不同的属性,就是说,当选择“按里程计价”时,就只看到distance这个字段,during就隐藏,而“按时间计价”被选择市,distance就隐藏,这里有种想实现动态显示field_set的感觉
    4 条回复    2015-04-13 00:33:21 +08:00
    twor2
        1
    twor2  
       2015-04-12 23:52:06 +08:00
    新手的一点思考

    在模板 html 里面用
    <% ifequal type '按里程计价' %>
    <div class='distance'></div>
    <% endifequal %>

    <% ifequal type '按时间计价' %>
    <div class='during'></div>
    <% endifequal %>

    但下拉框选,type 怎么赋值?

    估计不是django 的问题,而是js的问题
    tidewind
        2
    tidewind  
       2015-04-13 00:00:10 +08:00
    两个做法

    1:如果对价格实时性要求较高可以在view里接受页面提交的计价类型,对model里进行查询,页面select时发起ajax请求

    2:在view时将两种计费方式都render到页面上,可以存储到隐藏域或者js变量里,然后在select时分别显示这两个值

    一般情况下建议第二种,性能好一些。
    mornlight
        3
    mornlight  
       2015-04-13 00:14:27 +08:00 via iPhone
    我怎么感觉你说的效果是js 的任务
    14
        4
    14  
       2015-04-13 00:33:21 +08:00 via Android
    AngularJS
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1028 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:50 · PVG 03:50 · LAX 12:50 · JFK 15:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.