我对构建函数的方式有疑问。
我的函数呈现 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',)