【问题标题】:inserted value into model, with SelectWithPop, but page is not updated unless I restart the server使用 SelectWithPop 将值插入模型,但除非我重新启动服务器,否则页面不会更新
【发布时间】:2011-05-31 15:19:41
【问题描述】:

所以,我有一个带有 SelectWithPop 的表单。因此,用户可以选择一个变量,也可以再添加一个。 将打开一个新页面供用户输入数据。

我的问题是我可以在模型中插入一个新变量,但是当我返回页面选择一个值时,直到我重新启动服务器后才会显示新值。
这种行为是正常的还是我做错了什么?
如果是,有什么办法可以解决这个问题?

以下是我的观点:

def add(request, field):
    return handlePopAdd(request, HospitalForm, 'hospital_name')

def handlePopAdd(request, addForm, field):
    if request.method == "POST":
        form = addForm(request.POST)
        if form.is_valid():
            try:
                newObject = form.save()
            except forms.ValidationError, error:
                newObject = None
            if newObject:
               return HttpResponseRedirect(reverse('form', args=['clinical']))
    else:
        form = addForm()
    return render_to_response("popadd.html", { 'form': form })

表格:

class CheckPatForm(forms.Form):
    pat = forms.IntegerField(label="Paciente")

    _names = list(Hospital.objects.values_list('hospital_id', 'hospital_name'))
    _names.append(('',''))

    hosp = forms.ChoiceField(_names, widget=SelectWithPop(), label="Hospital", required=False)
    proc = forms.IntegerField(label="Processo", required=False)

class HospitalForm(forms.ModelForm):
    class Meta:
        model = Hospital                                        

模板添加:

<a
href="/SIAM-TB/insert/form/add/{{ field }}"
class="add-another"
id="add_id_{{ field }}">
    <img src="http://rome/SIAM-TB/admin_media/img/admin/icon_addlink.gif" 
         width="10" height="10" alt="Add Another"/>
</a>

【问题讨论】:

    标签: django django-forms django-views


    【解决方案1】:

    将获取医院查询集的代码放入 init 表单中。例如

    ....
    
    hosp = forms.ChoiceField(widget=SelectWithPop(), label="Hospital", required=False)
    
    def __init__(self, *args, **kwargs):
        super(CheckPatForm, self).__init__(*args, **kwargs)
        names = list(Hospital.objects.values_list('hospital_id', 'hospital_name'))
        names.append(('',''))
        self.fields['hosp'].choices = names
    

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 2012-11-12
      • 2013-07-17
      • 2020-09-05
      • 1970-01-01
      相关资源
      最近更新 更多