Hello.
First I should preface this by saying I am completely new to Django, HTML and web development as a whole. so any explanation would be very appreciated.
I am trying to develop a site where there is a for loop going through multiple object, and object generates a button. How do I make Django know which object the buttons relate to. Specifically this happens in two instances. for example approval:
views.py:
def userapproval(request):
if request.method == "POST":
form = Approval(request.POST)
if form.is_valid():
if form.decision == 'Approved':
form.related_id.change_status('Approved')
#else:
# todo Email (message?) with the explanation for the denial
return render(request=request, template_name='admin/user_approval.html',
context={"awaiting_approval": LabUser.objects.filter(approved=False),"form": Approval})
class Approval(forms.Form):
APPROVAL_CHOICES = [
('Approved', 'Approved'),
('Denied', 'Denied'),]
decision = forms.CharField(label='Approval:', widget=forms.RadioSelect(choices=APPROVAL_CHOICES))
reason = forms.CharField(max_length=100) # todo make mandatory if denied, disable otherwise
related_id = None
How do I know which user from the users outputted in the for the admin approved? Another minor question while I am at it, how do I make the reason charfield only mandatory if denied and outright disable it if approved.
So, it’s been a while since I did proper HTML, but I believe if you give a name and value to the button elements in HTML, you’ll only get sent the one that the user clicked - then, you can look for them in request.POST and work out which one was pressed.
Update: I just read the code and it appears you’re asking about the radio buttons, not button buttons - my mistake. You’ll get that value from form.cleaned_data['decision'] (Django handles decoding what the user picked for you).
As for the charfield being mandatory only if denied - your best bet is to write a custom clean function for it on the form, something like this:
def clean_reason(self):
if self.cleaned_data["decision"] == "Denied" and not self.cleaned_data["reason"]:
raise forms.ValidationError("Reason must be provided when denying")
return self.cleaned_data["reason"]
You can write those sorts of methods for any form field to do custom checks on them. Two things to look out for - they run in order (so this only works because you have the reason field after decision), and they need to return the new value of the field (so many times I have had fields end up with value None because I forgot to return the value)
and just like that I have the same issue. Each users gets his own line, and each user has gets Disable/Reset button. how do I know from which line of buttons was pressed.