Django - Показать ошибки формы в Dynami c Шаблон - PullRequest
0 голосов
/ 04 августа 2020

Я безуспешно пытаюсь получить ошибки формы в шаблоне Dynami c. Я знаю, что вместо этого

Это мой views.py:

def post_detail(request,slug):
    post = get_object_or_404(Post,slug=slug)
    commenti = Commento.objects.filter(post=post)
    commenta = CommentiForms()
    context = {"post":post,"commenti":commenti,"commenta":commenta}
    return render(request,'post_detail.html',context)



@login_required
def addComment(request,slug):
    post = get_object_or_404(Post,slug=slug)
    if request.method == 'POST':
        form = CommentsForms(request.POST)
        if form.is_valid():
            form.save(commit=False)
            form.instance.post = post
            form.instance.user = request.user
            form.save()
            url = reverse('post-detail',kwargs={'slug':slug})
            return HttpResponseRedirect(url)
    else:
        form = CommentsForms()
    return HttpResponseRedirect(url)

post_detail. html:

{% if request.user.is_authenticated %}
{% for comments in commenti %}
<li">
<div class="container">
<img class="omg-fluid" src="img/user.png" alt="">
<span>{{ comments.utente.username }}</span>
<span">{{ comments.data_commento }}</span>
</div>
<p>{{ comments.commento }}</p>
</li>
{% empty %}
<li>
<p>No Comments!</p>
</li>
{% endfor %}
{% include 'addComment.html' %}
{% else %}
<h1>You need to be logged for add a comment</h1>
{% endif %}

addComment. html:

<form action="{% url 'commenta' slug=post.slug %}" class="form" method="POST">
    {% csrf_token %}
    {% for fields in commenta %}
    {{ fields }}
    {% endfor %}
    <button type="submit">Post Comment</button>
</form>
...