0

我有一个从 context_processors 调用的choiceField,因此它可以出现在我网站的所有页面中。它提供数据库中现有的项目。问题是它没有正确刷新。我删除了数据库中的一些记录,它们仍然在我的选择中。此外,我希望它在用户创建新项目时自动刷新。

这是我的表格以及我如何称呼它。如果有人有建议,将不胜感激。

表格.py:

class SelectForm(forms.Form):

        def __init__(self, *args, **kwargs):
                super(SelectForm, self).__init__(*args, **kwargs)
                self.fields['project'].initial=[(p.proj_id, p.proj_name+"_"+p.proj_description) for p in Project.getProjectParent(Project())]


        project_choices = [(p.proj_id, p.proj_name+"_"+p.proj_description) for p in Project.getProjectParent(Project())]

        project = forms.ChoiceField(project_choices)

        def save(self):
                project = self.cleaned_data['project']
                src = ''
                p = {'proj': project, 'src': src}
                return p

context_processors.py:

def display_select_proj(request):
        if request.method == "POST" and (request.POST.get("action", "") == "Change"):
                form = SelectForm(request.POST)
                if form.is_valid():
                        p = form.save()
                        proj = p['proj']
                        src = p['src']

                        request.session['proj'] = proj

        else:
                proj = request.session['proj']
                src = ""
                form = SelectForm(initial={'project': proj})

        return {'select_form': form, 'proj': proj, 'src': src}
4

1 回答 1

3

对于第一部分,在initproject_choices内部移动,以便在调用表单时生成它,然后它将在刷新时更新。

于 2011-09-07T08:40:47.203 回答