Django动态形式 - 即时野外人口? [英] Django dynamic forms - on-the-fly field population?

查看:72
本文介绍了Django动态形式 - 即时野外人口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在扫描Django文档和Google搜索结果,所有的下午,我仍然有点困扰我尝试创建一个动态表单。我希望我只需要一个人来推动我正确的方向:-)我刚刚开始学习Django,所以我还是一个初学者,然而,我已经是一个中间的python用户。

I've been scanning through Django documentation, and Google search results, all afternoon and I'm still somewhat stuck in my attempt to create a dynamic form. I'm hoping I just need someone to nudge me in the right direction :-) I'm just starting to learn Django, so I'm still very much a beginner; however, I'm already an intermediate python user.

我想要做的是创建一个动态表单,用户从下拉列表中进行选择菜单,并且基于该选择,表单的另一部分将自动更新以显示与当前选择的项目相关的结果,但是从另一个数据库表。

What I'm trying to do is create a dynamic form, where the user makes a selection from a drop-down menu, and based on that selection another part of the form will automatically update to display results relevant to the currently selected item, but from another database table.

我会尝试并使用Django教程中的简化版模型来更好地说明我正在做的事情:

I'll try and use a simplified version of the models from the Django tutorial to better illustrate what I'm trying to do:

# models.py
from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)

所以我想说,我想要一个类似下拉选择字段的东西,填充数据库中每个投票的问题。我还想有一个文本字段,显示当前选定的轮询的相应选择,当用户选择不同的池时,它将随时更新。我已经能够通过放置一个按钮,并将信息发布回表单来计算出来;但是,我试图在用户进行选择时自动执行此操作。我现在看起来像这样:

So lets say I want to have something like a drop-down selection field, populated with the question from each Poll in the database. I also want to have a text-field, which displays the corresponding choices for the currently selected Poll, which will update on-the-fly whenever the user selects a different Pool. I've been able to figure this out by placing a button, and posting information back to the form; However, I'm trying to do this automatically as the user makes a selection. My view sort of looks something like this at the moment:

#view.py
from django import forms
from django.shortcuts import render_to_response

from myapp.models import Poll,Choice

class MyModelChoiceField(forms.ModelChoiceField):
    def label_from_instance(self, obj):
        return "%s" % obj.question

class PollSelectionForm(forms.Form):
    polls = MyModelChoiceField( queryset=Poll.objects.all() )

class ChoiceResults(forms.Form):
    def __init__(self, newid, *args, **kwargs):
        super(ChoiceResults, self).__init__(*args, **kwargs)

        self.fields['choice'] = forms.TextField( initial="" )

def main(request):
    return render_to_response("myapp/index.html", {
        "object": PollSelectionForm(), 
        "object2": ChoiceResults(),
    })

我的模板很简单,就像

{{ object }}
{{ object2 }}

我确定我打算创建表单的方式可能不是最好的,所以也可以自由地批评:-)正如我所提到的,我已经阅读了解决方案涉及到转发形式,但是我想要这样做即时发生,如果我可以透明地转发,那将是罚款我猜。我也看过可以让你动态创建表单的图书馆,但这似乎是过分的。

I'm sure the way I'm going about creating the forms is probably not the best either, so feel free to criticize that as well :-) As I mentioned, I've read solutions involving reposting the form, but I want this to happen on-the-fly... if I can repost transparently then that would be fine I guess. I've also seen libraries that will let you dynamically create forms, but that just seems like overkill.

推荐答案

这是一种方法 - Django / jQuery级联选择框?

Here is one approach - Django/jQuery Cascading Select Boxes?

您可以创建一个新的视图,只需将json呈现为一个字符串
,然后当您从第一个列表中完成选择时触发事件,该列表从该json动态加载数据。

You can create a new view that just renders json to a string, and then trigger an event when you're done selecting from the first list which loads the data dynamically from that json.

这篇关于Django动态形式 - 即时野外人口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆