2

我对构建函数的方式有疑问。

我的函数呈现 2 种形式。用于更改白板主图片的表单和用于从特定白板上删除图片的表单

两种形式都显示一个下拉框,列出所有供学生选择的白板以及学生何时选择白板。它显示所有对象。为白板设置主图片的表单非常有效,因为它显示了所有图片对象,但是当我从删除图片下的下拉框中选择一个值时。该函数不会返回删除图片标题下的所有图片对象,而是显示主要表单下的所有图片。

在此处输入图像描述 我认为问题出在我的 if forms.is_valid(): 和我的 if forms.is_valid(): 因为当提交表单时。它只会将 POST 放入 if forms.is_valid():

我的意见.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)})

我的 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 %}

我的表格.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',)
4

2 回答 2

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 %}
于 2013-03-27T06:40:08.557 回答
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>的底部

于 2013-03-27T06:38:26.743 回答