Django渲染模板对象错误

时间:2013-03-27 06:31:00

标签: django

我的功能构建方式存在问题。

我的功能呈现2个表格。用于更改白板主图片的表单和用于从特定白板删除图片的表单

两个表单都会显示一个保管箱,其中列出了学生可以选择的所有白板以及学生选择白板的时间。它显示所有对象。 为白板设置主图片的表单非常有效,因为它显示所有图片对象,但是当我从删除图片下的保管箱中选择一个值时。 该功能不会返回删除图片标题下方的所有图片对象,而是显示主要表单下的所有图片。

enter image description here 我认为问题出在我的if forms.is_valid():和我的if formss.is_valid():因为提交表单时。只有POST到if forms.is_valid():

my views.py

 def WhiteBoardEditor(request):
    if not request.user.is_authenticated():
    return HttpResponseRedirect(reverse('world:LoginRequest'))
picture = {}
pict = {}

if request.method == "POST":
        forms = BoardPictureForm(request.user,request.POST,)
        formss = PictureDeleteForm(request.user,request.POST,)

        if forms.is_valid():
            board = forms.cleaned_data['board']
            if board:
                boards = forms.cleaned_data['board']
                picture = Picture.objects.filter(board=boards)
        return render(request,'boardeditor.html',{'picture':picture,'boardpicture':BoardPictureForm(request.user),'picturedelete':PictureDeleteForm(request.user)})
        if formss.is_valid():
            pooh = formss.cleaned_data['board']
            if pooh:
                pooh = formss.cleaned_data['board']
                pict = Picture.objects.filter(board=pooh)
        return render(request,'boardeditor.html',{'pict':pict,'boardpicture':BoardPictureForm(request.user),'picturedelete':PictureDeleteForm(request.user)})


return render(request,'boardeditor.html',{'boardpicture':BoardPictureForm(request.user),'picturedelete':PictureDeleteForm(request.user)})

my boardeditor.html

 <h1> Set a primary picture for a whiteboard</h1>

 <form method="POST" enctype="multipart/form-data">
     {% csrf_token %}
    {{ boardpicture.as_p }}
    <input type = "submit" value= "save" />
 </form>

      {% for p in picture %}
 <li><a href ="{% url world:delpic p.id 1 %}">{{p.description}}</a>
 {% endfor %}





 <h1> Delete picture from whiteboard</h1>

 <form method="POST" enctype="multipart/form-data">
     {% csrf_token %}
    {{ picturedelete.as_p }}
    <input type = "submit" value= "save" />

 </form>

 </form>
 {% for pi in pict %}
 { pi.description }}
 {% endfor %}

my forms.py

 class BoardPictureForm(forms.ModelForm):

    def __init__(self, user, *args, **kwargs):
            super(BoardPictureForm, self).__init__(*args, **kwargs)
            self.fields['board'].queryset = Board.objects.filter(user=user)

class Meta:
    model = Picture 
    fields = ('board',)

 class PictureDeleteForm(forms.ModelForm):

    def __init__(self, user, *args, **kwargs):
            super(PictureDeleteForm, self).__init__(*args, **kwargs)
            self.fields['board'].queryset = Board.objects.filter(user=user)

class Meta:
    model = Picture 
    fields = ('board',)

2 个答案:

答案 0 :(得分:1)

Edit2:啊,我误解了这个问题 - 以下内容与问题无关,但对OP仍然有用

我怀疑这是因为你没有生成结构良好的HTML。尝试将boardeditor.html中的“图片”部分更改为

<ul>
{% for p in picture %}
    <li><a href="{% url world:delpic p.id 1 %}">{{p.description}}</a></li>
{% endfor %}
</ul>

编辑:同样,

{% for pi in pict %}
    {{ pi.description }} <!-- added an opening curly brace -->
{% endfor %}

你的底部有一个多余的</form>

答案 1 :(得分:1)

您的两个表单使用相同的字段board。这就是为什么当你提交第二个表格时,第一个表格是一个过程。

要解决您的问题,您需要在每个表单中指定操作。请注意,在我的回答中,我使用过程值以隐藏格式添加输入。在您的视图中,我为该过程创建if和else语句,以便在您提交表单时系统将知道必须执行哪个表单。

if request.method == "POST":
    forms = BoardPictureForm(request.user,request.POST,)
    formss = PictureDeleteForm(request.user,request.POST,)

    if request.POST['process'] == 'primary':
        if forms.is_valid():
            board = forms.cleaned_data['board']
            if board:
                boards = forms.cleaned_data['board']
                picture = Picture.objects.filter(board=boards)
        return render(request,'boardeditor.html',{
            'picture':picture, 
            'boardpicture':BoardPictureForm(request.user), 
            'picturedelete':PictureDeleteForm(request.user)
        })
    elif request.POST['process'] == 'delete':
        if formss.is_valid():
            pooh = formss.cleaned_data['board']
            if pooh:
                pooh = formss.cleaned_data['board']
                pict = Picture.objects.filter(board=pooh)
        return render(request,'boardeditor.html',{
            'pict':pict, 
            'boardpicture':BoardPictureForm(request.user), 
            'picturedelete':PictureDeleteForm(request.user
        )}

    elif request.POST['process'] == 'third':
        //other form here

<h1> Set a primary picture for a whiteboard</h1>

 <form method="POST" enctype="multipart/form-data">
     {% csrf_token %}
    {{ boardpicture.as_p }}
    <input type = "hidden" name="process" value= "primary" />
    <input type = "submit" value= "save" />
 </form>

      {% for p in picture %}
 <li><a href ="{% url world:delpic p.id 1 %}">{{p.description}}</a>
 {% endfor %}


 <h1> Delete picture from whiteboard</h1>

 <form method="POST" enctype="multipart/form-data">
     {% csrf_token %}
    {{ picturedelete.as_p }}
    <input type = "hidden" name="process" value= "delete" />
    <input type = "submit" value= "save" />

 </form>

 </form>
 {% for pi in pict %}
 { pi.description }}
 {% endfor %}