Не отображается форма Django на HTML странице
Вроде бы все сделал правильно, не вижу проблемы, может вы подскажите?
views.py:
from django.shortcuts import render, get_object_or_404, redirect
from .models import Item
from .forms import CommentForm
def product_detail(request, product_id):
product = get_object_or_404(Item, pk=product_id)
comments = product.comments.all()
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
new_comment = form.save(commit=False)
new_comment.user = request.user
new_comment.product = product
new_comment.save()
return redirect('product_detail', product_id=product_id)
else:
form = CommentForm()
return render(request, 'store/item_details.html', {'product': product, 'comments': comments, 'form': form})
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('product/<int:product_id>/', views.product_detail, name='product_detail'),
]
models.py:
from django.db import models
from django.contrib.auth.models import User
from store.models import Item
class Comment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
product = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='comments')
text = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
forms.py:
from django import forms
from .models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['text']
item_details.html:
<h2>{{ item.title }}</h2>
<p style="margin: 0em 0 1em 0em">
Товар належить категорії {% for tag in item.tags.all %}
<b><a href="{% url 'store:tag_details' tag.slug %}">{{ tag }}</a></b>
{% if not forloop.last %},{% endif %} {% endfor %}
</p>
{% if item.image %}
<p>
<span class="image left"><img src="{{ item.image.url }}" alt style="max-width: 250px; max-height: 350px" />
{% else %}
<p>
<span class="image left"><img src="{% static 'images/pic14.jpg' %}?v=1.0" alt />
{% endif %}
<a href="{% url 'cart:add_to_cart' item.slug %}" class="button small" style="margin-top: 10px">
Додати у корзину</a>
</span>
{{ item.description }}{{ item.description }}
</p>
{% if item.old_price %}
<p>
Стара ціна: <s>{{ item.old_price }} гривень</s> Нова ціна:
<b>{{ item.price }}</b> гривень
</p>
{% else %}
<p>Ціна: <b>{{ item.price }}</b> гривень</p>
{% endif %}
</p>
<h2>Comments:</h2>
<ul>
{% for comment in comments %}
<li>{{ comment.text }} - {{ comment.user.username }} ({{ comment.created_at }})</li>
{% empty %}
<li>No comments yet.</li>
{% endfor %}
</ul>
<h2>Add a comment:</h2>
<form method="post">
{% csrf_token %}
{{ form }}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
Буду благодарен, если поможете найти ошибку!