Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 从列表中选择用户并添加到post请求_Django_Database_Django Templates - Fatal编程技术网

Django 从列表中选择用户并添加到post请求

Django 从列表中选择用户并添加到post请求,django,database,django-templates,Django,Database,Django Templates,我有一个用户列表,旁边都有一个复选框。所有选中的用户都应添加到数组中,然后通过Django post请求发送到数据库 下面是一张照片: 底部的按钮是submit按钮 型号: class UserProfile(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) color = models.CharField(max_length=25, default="", blank=True)

我有一个用户列表,旁边都有一个复选框。所有选中的用户都应添加到数组中,然后通过Django post请求发送到数据库

下面是一张照片:

底部的按钮是submit按钮

型号:

class UserProfile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    color = models.CharField(max_length=25, default="", blank=True)

class Studyplan(models.Model):
    name = models.CharField(max_length=100, null=True)
    description = models.CharField(max_length=1000, null=True)
    parent_assignment = models.ForeignKey(Assignment, blank=True, on_delete=models.CASCADE,related_name="studyplan", null=True)
    deadline = models.DateField(default=date.today, null=True)
    students = models.ManyToManyField(UserProfile, blank=True, related_name="studyplan_students", null=True)
    teachers = models.ManyToManyField(UserProfile, blank=True, related_name="studyplan_teachers", null=True)
    canview = models.ManyToManyField(UserProfile, blank=True, related_name="studyplan_canview", null=True)
    notes = models.ManyToManyField(Note, blank=True, related_name="studyplan", null=True)
    tasks = models.ManyToManyField(Task, blank=True, related_name="studyplan", null=True)
    messages = models.ManyToManyField(Message, blank=True, related_name="studyplan", null=True)
观点:

@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')
我的问题是,我无法将值从ManyToManyField中获取到POST请求中

我得到了一个类似的错误:禁止直接分配到多对多集合的前端。使用电子邮件\u获取帮助。改为使用set()

选择模式:

{% 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 %}
{%for elev in elever%}
{{elev.user | capfirst | first}}
{{elev.user | capfirst}}
切克南先生{
左边距:10px
}
.checkboxx{
对齐内容:柔性端;
左边距:300px
}
{%endfor%}
整个HTML:


假设您的
请求.POST
包含键
canview
的用户名列表,您将得到如下用户名列表:
usernames=request.POST.getlist('canview')

现在,使用用户名列表,您需要获取相应的用户,并使用
set()
将其分配给M2M字段:


您可以删除HTML的图片,然后将选择小部件粘贴到用户被选中的位置吗?其余的都无关紧要。你能说出你在问哪个领域吗?您的模型中有许多字段,我们不知道您试图为HTML表单中选择的用户分配哪一个字段。http.post中的canview字段,我需要一个数组,将我选择的所有用户输入其中。我将粘贴选择小部件!所以,如果您查看浏览器开发工具“网络”选项卡,您的POST请求是什么样子的?查看提交的值。还可以查看调试器中的
request.POST
。正如错误所述,您不能直接将值分配给M2M字段,您必须使用
set()
add()
。请阅读并练习了解如何使用M2M关系。我已经试了好几个小时了,你能举个例子吗?
usernames = request.POST.getlist('canview')
users = User.objects.filter(username__in=usernames)
studyplan = Studyplan(name=name, description=description, parent_assignment=parent_assignment)
studyplan.save()  # you need a saved item to be able to create the M2M relationship
studyplan.canview.set(users)