开发者

Django : refresh issue on selection field

I have a choiceField called from a context_processors so it can appear in all of my site's pages. It provides the projects existing in a database. The problem is that it does not refresh correctly. I deleted some records in my database and they still are in my selection. Also I'd like it to refresh automatically when a user creates a new project.

Here is my form and how I call it. If anyone has a suggestion, it would be appreciated.

forms.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}


for the first part move project_choices inside init so it is generated as the form is called, it will then update on refresh.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜