I am hoping my question makes sense.
I am trying to update a django form which I have in a function based view.
Slight twist. This form is rendered on a template handled by another function.
For some reason, the title
field entered in the existing form is erased when submitting the updated form.
I thought the code, somewhere, was re-creating a blank form when calling the update function. However, other fields (where a FK or a M2M is involved are actually updated an not erased after submit. As an example, if I add another user to the m2m relationship, the previous user will still be recorded on the updated form.
I tried different solutions, below is my lastest iteration. There is no error message appearing, so there must be some logic somewhere that is causing the issue.
What I am trying to achieve:
When a request.user clicks the “add button” on template, the userprofile_id is added to the model object: this part works.
The problem is that the text that comes with this object model is blank.
model
class Voucher(models.Model):
user = models.ManyToManyField(User, blank=True)
venue = models.ForeignKey(Venue, blank=True, null=True, related_name="vouchervenues", on_delete=models.CASCADE)
title = models.TextField('voucher title', blank=True)
url
path('venue_loyalty_card/<userprofile_id>', views.venue_loyalty_card,name="venue-loyalty-card"),
path('allocate_voucher/<userprofile_id>/<voucher_id>',views.allocate_voucher, name ="allocate-voucher"),
form
class VoucherForm(ModelForm):
class Meta:
model = Voucher
fields = ('title')
views
def venue_loyalty_card(request, userprofile_id):
venue = UserProfile.objects.filter(user=request.user).values('venue')
venue_vouchers_available = Voucher.objects.filter(venue=request.user.userprofile.venue)
form=LoyaltyCardForm()
voucherform=VoucherForm()
updateform=VoucherForm()
return render(request,"main/account/venue_loyalty_card.html",{'venue':venue,'venue_vouchers_available':venue_vouchers_available,'updateform':updateform})
def allocate_voucher(request, userprofile_id,voucher_id):
url = request.META.get('HTTP_REFERER')
venue = UserProfile.objects.filter(user=request.user).values('venue')
userprofile = get_object_or_404(UserProfile, pk=userprofile_id)
voucher = get_object_or_404(Voucher, pk=voucher_id)
if request.method == "POST":
updateform = VoucherForm(request.POST, instance = voucher)
if updateform.is_valid():
data = updateform.save(commit=False)
data.save()
updateform.save_m2m()
current_voucher_instance = get_object_or_404(Voucher, id=data.id)
current_voucher_instance.user.add(userprofile.id)
return redirect(url)
else:
print(updateform.errors)
else:
updateform = VoucherForm()
return redirect('venue-loyalty-card',{'userprofile':userprofile,'voucher':voucher,'venue':venue})
template (main/account/venue_loyalty_card.html)
{%for voucher in venue_vouchers_available%}
{{voucher.id}}
{{voucher}}
<form action="{%url 'allocate-voucher' userprofile.id voucher.id%}" method="POST">
{% csrf_token %}
<input type="Submit" value="Add" class="btn btn-primary custom-btn">
</form>
{%endfor%}