Django 动态表单 - 动态字段填充? [英] Django dynamic forms - on-the-fly field population?

查看:20
本文介绍了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)

所以假设我想要一个下拉选择字段,填充数据库中每个民意调查的问题.我还想要一个文本字段,它显示当前选择的 Poll 的相应选项,每当用户选择不同的 Pool 时,它就会即时更新.我已经能够通过放置一个按钮并将信息发布回表单来解决这个问题;但是,我正在尝试在用户进行选择时自动执行此操作.我的观点目前看起来像这样:

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 级联选择框?

您可以创建一个新视图,将 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天全站免登陆