向ManyToManyField发出Django发布请求

时间:2019-09-09 16:32:07

标签: html django django-models django-forms django-templates

这是我在HTML文档中的列表 enter image description here 我有一个Django帖子视图,当我点击send时,我希望将选定的用户发布到学生字段。

如您所见,我已经尝试从this url开始执行实例和用户操作,但是我无法弄清楚!

HTML:

                        {% for elev in elever %}
                        <div class="custom-control custom-checkbox">

                            <span class="d-flex align-items-center">


                                <div style="margin-right: 10px">
                                    <div class="circle" style="background-color: {{ elev.color }}">
                                        <span class="initials" , style="color: #ffffff"> {{ elev.user|capfirst|first }}
                                        </span>
                                      </div>
                                </div>

                              <div style="width: 300px">

                                <span class="h6 mb-0" data-filter-by="text">{{elev.user|capfirst}}</span>
                              </div>
                              <div class="checkboxx"> <input type="checkbox" name="Studyplan-Canview" value= "{{ student.name }}">
                                <style>
                                .checkerName {
                                  margin-left: 10px
                                }

                                .checkboxx {
                                  align-content: flex-end;
                                  margin-left: 300px
                                }
                                </style> </div>

                            </span>
                          </label>
                        </div>
                        {% endfor %}

视图:

#post request checker
@login_required
def skapa_studieplan(request):
  if request.method == 'POST':

    name = request.POST.get('Studyplan-Name')
    description = request.POST.get('Studyplan-Description') 
    parent_assignment = Assignment.objects.get(pk=request.POST.get('Studyplan-PAID'))
    users = UserProfile.objects.all()
    instance = Studyplan.objects.create(request.POST.get('Studyplan-Canview'))
    for user in users:
        instance.canview.add(user)

    studyplan = Studyplan(name=name, description=description, parent_assignment=parent_assignment, canview=instance)
    studyplan.save()

    return redirect('allaStudieplaner')

0 个答案:

没有答案