帖子评论

时间:2019-11-29 10:07:06

标签: python django

我无法让我的评论显示在可以将罚单保存到数据库的票证中,而且我可以看到帖子中有多少评论,但不会发表评论。 型号:

class Ticket(models.Model):
    id = models.AutoField(primary_key=True, unique=True, auto_created=True)
    staffmember = models.ForeignKey('users.Users', verbose_name='Users',on_delete=models.CASCADE, default=True,
                                    related_name='ticket')
    ticketId = models.UUIDField(default=uuid.uuid4, editable=False)
    ticketName = models.CharField(max_length=200)
    ticketDescription = models.TextField(max_length=10000)
    ticketTime = models.DateTimeField(default=timezone.now)
    role = models.CharField(max_length=40, choices=Roles, default=developer)
    condition = models.CharField(max_length=40, choices=Condition, default=Opened)
    priority = models.CharField(max_length=40, choices=priority, default=low)



class Comment(models.Model):
    id = models.AutoField(primary_key=True, unique=True, auto_created=True)
    IdTicket = models.ForeignKey('ticket.Ticket', verbose_name='Ticket', on_delete=models.CASCADE, related_name='comment')
    user = models.ForeignKey('users.Users',  verbose_name='Users', on_delete=models.CASCADE,
                               related_name='comments')
    timestamp = models.DateTimeField(default=timezone.now)
    description = models.CharField(max_length=1000)

观看次数:

class EditTicketView(UpdateView, LoginRequiredMixin):
        model = Ticket
        post_form_class = EditTicketForms
        comment_form_class = CommentForm
        template_name = 'editTicket.html'
        fields = ['ticketName', 'ticketDescription', 'condition', 'priority', 'role']
        success_url = reverse_lazy('dashboard')


def CreateCommentView(request, pk):
    post = get_object_or_404(Ticket, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            Comment = form.save(commit=False)
            Comment.ticketId = post
            #form.cleaned_data('description')
            Comment.save()
            return redirect('viewComment', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'Createcomment.html', {'form': form})

HTML: 我尝试在单击票证时显示评论,但不确定我是否要正确处理,因为我提到了票证工作的评论数(例如,在票证2上,票证有1条评论) 1就是20)

{% extends 'base.html' %}

{% block title %}Edit Ticket{% endblock %}
<!--Displays the view depending on who is logged in-->
{% block content %}
  <h1>Package Details</h1>
      {% if request.user == object.staffmember or user.is_Staff %}
  <form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
     <input class="btn btn-success" type="submit" value="Edit ticket">
      {% else %}
          <span class="font-weight-bold">{{ "Title: " }}{{ object.ticketName }}</span>
       <div class="card-body">
      <p>{{ "Issue: " }}{{ object.ticketDescription}}</p>
      <p>{{ "time made: " }}{{ object.ticketTime }}</p>
      <p>{{ "priority: " }}{{ object.priority }}</p>
      <p>{{ "status: " }}{{ object.condition }}</p>
      <p>{{ "Posted by: " }}{{ object.staffmember }}</p>
     <a class="btn btn-default" href="{% url 'dashboard' %}">back to dashboard</a>
<script>
function goBack() {
  window.history.back();
}
</script>

          {% endif %}
</div>
</form>

        <hr>
     <form method="post">
    <a class="btn btn-default" href="{% url 'add-comment' ticket.id %}">Add comment</a>
{% for post in comment_list.all %}
    <div class="card-body">
        <p>{{ "Test" }}{{ post.timestamp }}</p>
        <strong>{{ post.user }}</strong>
        <p>{{ post.description | linebreaks}}</p>
    </div>
    {% empty %}
    <p>No comments here yet :(</p>
    {% endfor %}
</form>
    {% endblock %}

在编辑票证页面上的所有操作均显示“此处暂无评论”

1 个答案:

答案 0 :(得分:0)

您的Constraint.includes(:vars).where(active: true).where('vars.model_type IN ?', ['Model1',Model2'])循环应如下所示:

for