Reputation: 103
I have a micro ticket system. The user has information about the ticket, a button to mark as solved/mark as waiting and a message box to add new messages in to the ticket for the admin.
You can ignore the most stuff in the forms, important are the GET/POST requests of the forms.
\\views.py
def ticket_system_view(request, id):
obj = Ticket.objects.get(id=id)
waiting_message = "Mark as 'Waiting'"
solved_message = "Mark as 'Solved'"
if obj.reopened_counter > 5:
solved_message = 'Ticked solved forever'
waiting_message = 'Ticked solved forever'
button_form = TicketSolved(request.POST)
time.sleep(2)
if button_form.is_valid:
obj.reopened_counter += 1
if obj.reopened_counter > 5:
obj.ticket_waiting = True
obj.ticket_solved = False
if obj.ticket_waiting == False and obj.ticket_solved == True:
obj.ticket_waiting = True
obj.ticket_solved = False
else:
obj.ticket_waiting = False
obj.ticket_solved = True
obj.save()
user_form = TicketMessagesForm(
request.POST, instance=TicketMessages(id=id))
if user_form.is_valid():
instance = user_form.save(commit=False)
#this form doesnt work yes
instance.save()
return render(request, 'ticket-system.html', {'obj': obj, 'waiting_message': waiting_message, 'solved_message': solved_message, 'button_form': button_form, 'user_form': user_form})
\\forms.py
class TicketSolved(forms.Form):
delete = forms.CharField(
label='', max_length=0).widget = forms.HiddenInput()
class TicketMessagesForm(forms.ModelForm):
class Meta:
model = TicketMessages
fields = (
'user_message',
)
\\models.py if you need
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()
creator_adress = models.GenericIPAddressField(null=True)
start_date = models.DateTimeField(default=timezone.now)
ticket_waiting = models.BooleanField(default=True)
ticket_solved = models.BooleanField(default=False)
reopened_counter = models.IntegerField(default=0)
class TicketMessages(models.Model):
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE, default=None)
admin_message = models.TextField(null=True, default=None)
user_message = models.TextField(null=True, default=None)
Each time the user sets the ticket to Solved/Waiting, a counter counts. When the counter reaches 5, the ticket is automatically marked as solved to avoid spam.
Problem: I have Two forms in the view -user_form for the messages of the user -button_form for the form which contains only the counter button
Actually I don't need a form for button_form, but that doesn't matter for now.
If I now send user_form, button_form is also sent and so the counter counts up. How do I change my code so that when I send user_form, only user_form is sent and button_form is not? And the other way around. There more problems like the not working user_form but first things first.
Thank you very much! :^)
Edit: Here is the HTML File where display the forms
{% 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 %}
{{ button_form }}
<button {% if obj.reopened_counter > 5 %} disabled="" {% endif %} type="submit">
{% if obj.ticket_waiting == True %}
{{ solved_message }}
{% else %}
{{ waiting_message }}
{% endif %}
</button>
</form>
<p> Changes: {{ obj.reopened_counter }} </p>
<br>
<h2>New Message</h2>
<form method="POST">
{% csrf_token %}
{{ user_form }}
<button type="submit">New Message</button>
</form>
Upvotes: 0
Views: 698
Reputation: 10136
You can create a separate template for your second form with separate route and use include template tag to include that template in your main template like this:
second_template.html:
<form method="POST" action='second_view/'>
{% csrf_token %}
{{ user_form }}
<button type="submit">New Message</button>
</form>
urls.py:
path('second_view', views.second_view, name='second_view')
views.py:
def second_view(request, id):
user_form = TicketMessagesForm(
request.POST, instance=TicketMessages(id=id))
if user_form.is_valid():
instance = user_form.save(commit=False)
#this form doesnt work yes
instance.save()
return render(request, 'second_template.html', {'user_form': user_form})
And the last piece of puzzle is to include your second_template in your ticket_system so replace the second form with {% include second_template.html %}
. You can pass id to form just like you pass it in your original template.
Upvotes: 1