Reputation: 1943
I have a problem with the way I structured my function.
My function renders 2 forms. A form to change the primary picture for a whiteboard and a form to delete a picture from a particular whiteboard
Both forms displays a dropbox that list all whiteboards for the students to pick and when the students pick a whiteboard . it's displays all the objects. The form that set a primary picture for a whiteboard works perfectly because it display all picture objects but when I choose a value from the dropbox under the delete picture. The function doesn't return all the pictures objects underneath the delete picture header but it instead displays all the picture under the primary forms.
I think the problem is with my if forms.is_valid(): and my if formss.is_valid(): because when a form is submitted . It only get POST into the 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',)
Upvotes: 2
Views: 569
Reputation: 22808
Your two forms use the same field which is the board
. That's why when you submit the second form, the first form is the one process.
To fix your problem, you need to specify the action in every form. Notice that in my answer, I add input in hidden format with the process value. And in your view, I create if and else statement for that process so that when you submit the form the system will know which form must be executed.
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 %}
Upvotes: 1
Reputation: 2958
Edit2: Ah, I've misunderstood this question - the following isn't relevant to the question, but still useful to the OP
I suspect it's because your not generating well-formed HTML. Try changing the "picture" part in boardeditor.html to be
<ul>
{% for p in picture %}
<li><a href="{% url world:delpic p.id 1 %}">{{p.description}}</a></li>
{% endfor %}
</ul>
Edit: also,
{% for pi in pict %}
{{ pi.description }} <!-- added an opening curly brace -->
{% endfor %}
And you have a redundant </form>
towards the bottom
Upvotes: 1