Home > Enterprise >  Object_list doesn't show the correct template
Object_list doesn't show the correct template

Time:08-06

I have a Django project where users can ask questions and receiving answers. I have three models: Question, Answer and Comment. I don't know why comment template doesn't show the correct data, I dont't hnow where to find the comment data either object.comment, object.comment_set.all or anything else.

I had the same problem with fetching Answer data, but I successfully solved it by using '{% for answer in object.answer_set.all %}', but the same method doesn't apply in comment. I noticed that I don't understand where is all the information stucks to retrieve.

I'm relatively new to Django, so I'd be grateful to get all answers with description, why am getting this to avoid this in the fitire projects.

models.py

class Question(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=255, unique=True, db_index=True,     verbose_name="URL")
    detail = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('detail', kwargs={'slug': self.slug})


class Answer(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
    detail = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.detail


class Comment(models.Model):
    answer = models.ForeignKey(Answer, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE,     related_name='comment_user')
    detail = models.TextField(default='')
    date_posted = models.DateTimeField(default=timezone.now)

views.py

class AnswerView(ListView):
    model = Answer
    template_name = 'forum/detail.html'

class CommentView(ListView):
    model = Comment
    template_name = 'forum/detail.html'

detail.html

<div >
        {% for answer in object.answer_set.all %}
        <p>{{ answer.detail }}</p>
        <p><a href="#" >{{ answer.user.username }}</a>
        <span>5 комментариев</span></p>
        {% endfor %}
    </div>


            <!-- Comment Section Starts-->
        <h3 >Комментарии:</h3>
        {% for comment in comment.answer.all %}
            <div >
                <div >
                    <p>{{ comment.detail }}</p>
                    <p><a href="#">{{ comment.user.username }}</a></p>
                </div>
            </div>
        {% endfor %}

CodePudding user response:

Your comments are connected to answers in a many-to-one relation, so you can get them in the same way as you access answers from questions - you just need to do it within a nested loop, i.e.

{% for answer in object.answer_set.all %
// your answer template stuff

    {% for comment in answer.comment_set.all %}
    // your comment template stuff
    {% endfor %}

{% endfor %}

CodePudding user response:

you need use object_list in your template has the docs of ListView says

<div >
    {% for answer in object_list %}  <!-- change to object_list-->
        <p>{{ answer.detail }}</p>
        <p><a href="#" >{{ answer.user.username }}</a>
        <span>5 комментариев</span></p>
    {% endfor %}
</div>

if need/wanna change the name of the variable object_list, you can define context_object_name in the view.

  • Related