Ed Snowden Ed Snowden - 3 years ago 195
Python Question

Incorrect view Django

I have a GET part of the view:

def get(self, request, event_id, *args, **kwargs):
form = self.form_class(initial={})
evnt = Event.objects.get(id=event_id)
cmnt = Comments_events.objects.filter(event_cmnt_id=event_id)

ANSWR = HOW TO GET?

return render(request, self.template_name, {'event': evnt, 'comments': cmnt, 'answers': ANSWR, 'form': form})


Two models:

class Comments_events(models.Model):

text_cmnt = models.TextField

author_cmnt = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=True,
blank=True
)

class Answers(models.Model):

comment_answr = models.ForeignKey('Comments_events')

text_cmnt = models.TextField

author_cmnt = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=True,
blank=True
)


On the page I post a post, a comment to it, and I want to display the answers to the comments.

Tell me how to correctly make GET "answr" in a view to display it in html?

Answer Source

you can simple use related _set in the template, example:

{% for comment in comments %}
  {% for answer in comment.answers_set.all %}
      {{ answer }}
  {% endfor %}
{% endfor %}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download