Hello, why does my template Django not want to display the results of my review form?
I cannot understand why the result of my review form is not showing in the template, even though my ticket form is working fine. However, I have linked the 2 models with a foreign key. Then in the template I added "ticket.id". Finally, the objective of this app is to limit the review to one person. Thank you in advance for your help.
My models:
class Ticket(models.Model):
title = models.CharField(max_length=128)
content = models.TextField(max_length=2048, blank=True, null=True)
image = models.ImageField(null=True, blank=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
date_created = models.DateTimeField(auto_now_add=True)
def __str__(self) :
return self.title
class Review(models.Model):
ticket = models.ForeignKey(Ticket, related_name="reviews" ,on_delete=models.CASCADE)
rating = models.PositiveIntegerField(validators=[MinValueValidator(0),MaxValueValidator(5)])
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
headline = models.CharField(max_length=128)
body = models.TextField(max_length=8192, blank=True, null=True)
time_created = models.DateTimeField(auto_now_add=True)
def __str__(self) :
return self.ticket.title
My forms :
class TicketForm(forms.ModelForm):
class Meta:
model = models.Ticket
fields = ['title', 'content','image']
class ReviewForm(forms.ModelForm):
class Meta:
model = models.Review
fields = ['rating','headline','body']
My view :
@login_required
def review(request, ticket_id):
ticket = get_object_or_404(models.Ticket, id=ticket_id)
review_form = forms.ReviewForm(instance=ticket)
if request.method == 'POST':
review_form = forms.ReviewForm(request.POST, instance=ticket)
if review_form.is_valid():
review = review_form.save(commit=False)
review.user = request.user
review.save()
return redirect('home')
return render(request, 'flux/create_review_post.html', context={'review_form': review_form})
My urls:
path('home/', flux.views.home, name='home'),
path('ticket/create/', flux.views.ticket, name='ticket_create'),
path('flux/<int:ticket_id>/', flux.views.view_ticket, name='view_ticket'),
path('<int:ticket_id>/review/create/', flux.views.review, name='review_create'),
My template review form:
<form method="post" >
{{ review_form.as_p }}
{% csrf_token %}
<button class="btn btn-primary m-2 "
MY templat Html:
{% extends 'base.html' %}
{% block content %}
<a class="btn btn-dark m-2 " href="{% url 'ticket_create' %}">Create a ticket</a>
<h2 class="m-2">Flux</h2>
<div class="grid-container">
{% for ticket in tickets %}
<a class="btn btn-warning m-2 " href="{% url 'view_ticket' ticket.id %}"></a>
<div class="post border border border-secondary rounded m-2">
<div class="d-flex flex-row">
<p class="p-2 h5 text-primary">{{ ticket.user }}</p>
<p class="p-2 h5 text-primary">{{ ticket.id }}</p>
<p class=" p-2 ms-auto">{{ ticket.date_created }} </p>
</div>
<h4 class="p-2">Ticket : {{ ticket.title }}</h4>
{% if ticket.image %}
<img class="p-2" src="{{ ticket.image.url }}">
{% endif %}
<a class="btn btn-dark m-2 " href="{% url 'review_create' ticket.id %}">Create a review</a>
</div>
{% endfor %}
{% for review in reviews %}
<p class="p-2 h5 text-warning">{{ review.rating }}</p>
{% endfor %}
</div>
{% endblock content %}