Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHien Ha
1,417 PointsStuck with ManyToMany relationship
Hello I try to make the detail view with ManyToMany relationship
My urls: path('detail/<int:question_id>', views.DetailView, name = 'detail'), My models: from django.db import models class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField() def get_detail(question_id): return Question.objects.get(pk=question_id)
class Choice(models.Model): question = models.ManyToManyField(Question) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)
My views:
def DetailView(request, question_id):
q = Question.get_detail(question_id)
choices = Choice.objects.filter(question__pk = q.id)
context = {"qs":q, "choices":choices}
return render(request, "detail_question.html", context)
my template:
{% extends "base.html" %} {% block content123 %} {% if qs %} <h1>{{ qs.question_text }}</h1> <form action=""> <ul> {% for choice in choices %} <li><input type="radio" value="{{ qs.id }}" name="choice">{{ choice.choice_text }}<li> {% endfor %} </ul> <p><input type="submit" name="submit"</p> </form> {% endif %}
{% endblock %}
However after make mirgations and runserver, the page render only the Question, it does not render any choices
Alex Koumparos
Python Development Techdegree Student 36,887 PointsAlex Koumparos
Python Development Techdegree Student 36,887 PointsHi Hien,
Please remember to always use markdown to properly format your code.