Answer the question
In order to leave comments, you need to log in
How to add a comment without going to another page?
Hello, there is a form for adding reviews, but I don’t know how to display it under the product. I only did so that you could follow the link and fill out a review on another page.
Product reviews:
{% for comment in product.comments.all %}
<div class="col-md-12 ">
<strong class="comments">{{ comment.user }}</strong>
<div class="time">
Опубликованно: {{ comment.created|date:"G:i | d-m-Y " }}
</div>
<h4 class="imeno_comment">Отзыв: {{ comment.comment }}</h4>
{% if comment.image %}
<img src="{{ comment.image.url }}" class="otziv_img" alt=""/>
{% endif %}
<hr>
</div>
{% empty %}
<p>Пока нет отзывов :(</p>
{% endfor %}
{% if user.is_active %}
<div class="btn-pos">
<label class="hvr-skew-backward ">
<a class="add_comment"
href="{% url 'products:add_comment' slug=product.slug %}">Добавить
отзыв</a>
</label>
</div>
{% else %}
<h4 align="center">Авторизуйтесь, чтобы оставить отзыв</h4>
{% endif %}
url(r'^(?P<slug>[\w-]+)/comment/$', views.add_comment, name='add_comment'),
def add_comment(request, slug):
form = CommentForm(request.POST, request.FILES)
product = get_object_or_404(Product, slug=slug)
if request.method == 'POST':
comment = form.save(commit=False)
comment.product = product
comment.user = request.user
comment.save()
return redirect('products:detail', slug=product.slug)
else:
form = CommentForm()
template = "products/comment.html"
context = {'form': form}
return render(request, template, context)
{% block content %}
<h1>Оставьте свой отзыв</h1>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Отправить</button>
</form>
{% endblock %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question