Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

python - Show person list based on group selection list in django

I'm pretty new in django/ Python and any help will be appreciated

I'm trying to populate a list of person based on click on another list (Group)

Model:

class Grupo(models.Model):
    Nome = models.CharField(max_length=20)

View

class GruposListView(ListView):
    model = Grupo
    template_name = 'reports/relatorio_form.html'
    context_object_name = 'Grupo'

HTML

    <h4 class="mb-3">Select a group:</h4>
    <select id="Group" name="Group" size="5">
        {% for Grupo in Grupo %}
        <option value="Grupo">{{Grupo.Nome}}</option>
       {% endfor %}
    </select><br><br>

Here the result on the first list:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You needs rather some good tutorial instead of this answer.

HTML should have <form> and <button> to send selected group to server (ie. to other class GroupDetailView), and serve should get in as argument using

 re_path(r'^group/(?P<group_number>d+)$', views.GroupDetailView.as_view(), name='group-detail'),

and

 class GroupDetailView(...):

    def group_detail_view(request, group_number):

and use it to get persons from selected group

    def group_detail_view(request, group_number):

        persons = Person.objects.get(group=group_number)

and generate page with these person.

    def group_detail_view(request, group_number):

        persons = Person.objects.get(group=group_number)

        return render(request, 'group_detail.html', context={'persons':  persons})

All based on tutorial Django Tutorial Part 6: Generic list and detail views


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...