'Django set Boolean to False when clicking a button
The following code (just to learn Django) allows to mark the user's ticket as "Solved" with an HTML button. When the button is clicked, ticked_solved is set to True, ticket_waiting is set to False.
This is also recognized in the template, which I have tested. But then in the dashboard the object would have to change to False/True as well, which as you can see in the screenshot is not the case.
I do all this with a form, which is probably not the smartest option either.
\\ models.py
class Ticket(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
default=None,
null=True,
on_delete=models.CASCADE,
)
title = models.CharField(max_length=200)
description = models.TextField()
ticket_waiting = models.BooleanField(default=True)
ticket_solved = models.BooleanField(default=False)
def __str__(self):
return str(self.title)
\\forms.py
class TicketSolved(forms.Form):
delete = forms.CharField(
label='', max_length=0).widget = forms.HiddenInput()
\\views.py
def ticket_system_view(request, id):
obj = Ticket.objects.get(id=id)
form2 = TicketSolved(request.POST)
if request.method == 'POST':
if form2.is_valid():
obj.ticket_waiting = False
obj.ticket_solved = True
return render(request, 'ticket-system.html', {'obj': obj, 'form2': form2})
\\ .html
{% if obj.user != request.user %}
<p>Page Not Found</p>
{% else %}
<p>Ticket ID {{obj.id}}</p>
{{ obj.title }} {{ obj.description }} {% endif %}
<br></br>
<form method="POST">
{% csrf_token %} {{ form2 }}
<button type="submit">Status to Solved</button>
</form>
----Just to test if it works:
{% if obj.ticket_waiting == True %}
<p>Waiting for Reply.</p>
{% else %}
<p>Ticket solved.</p>
{% endif %}
\\urls.py if needed
path('dashboard/user/ticket/<int:id>/',
ticket_system_view, name="view_ticket"),
And a screenshot how it looks after clicking the button screenshot
Do you know why in the admin dashboard the checkboxes don't adjust?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
