将用户添加到 Django 中的特定组
Adding users to the specific group in Django
我正在尝试在我的 Django 项目中将一些用户添加到我之前创建的组中,但我知道如何添加到我知道名称并且不是动态的特定组。我希望当我 select 一个特定的组时,已经添加了下一个用户。问题在这里:
g = Group.objects.get(name= 'New')
我应该怎么做才能替换这个并指出我点击的这个组?
views.py:
@login_required
def choose_group(request, pk):
if request.method == "POST":
cvs = get_object_or_404(Cv, pk=pk)
p = Person.objects.create(name=cvs.author)
g = Group.objects.get(name= 'New')
m = Membership.objects.create(person=p, group=g, leader=False)
return redirect( 'proj.views.cv_detail', pk=cvs.pk )
else:
cv = Cv.objects.filter(author = request.user)
cvs = get_object_or_404(Cv, pk=pk)
mem = Membership.objects.all()
context = {
'mem':mem,
'cvs':cvs,
'cv':cv
}
return render(request, 'choose_group.html', context)
models.py:
class Person(models.Model):
name = models.CharField(max_length=128)
def __str__(self): # __unicode__ on Python 2
return self.name
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
def __str__(self): # __unicode__ on Python 2
return self.name
class Membership(models.Model):
person = models.ForeignKey(Person)
leader = models.BooleanField(default=False)
group = models.ForeignKey(Group)
choose_groups.html:
{% block profile %}
<div class="jumbotron">
<h3>Choose a group to add:</h3>
</div>
<ul>
{% for m in mem %}
<form method="POST" class="post-form" >{% csrf_token %}
<li><button type="submit" class="li1"> <b>{{ m.group }}</b></li>
</form>
{% endfor %}
</ul>
{% endblock %}
我在这里看到的主要问题是您没有从 html 发送群组名称 m.group
。
为此,您必须将名称和值添加到 html 中的按钮元素(并且不要忘记关闭按钮元素)
<button type="submit" class="li1" name="group" value="{{m.group}}"> <b>{{ m.group }}</b></button>
然后,您可以使用 request.POST
在 views.py 中检索此组值
g = Group.objects.get(name=request.POST['group'])
我正在尝试在我的 Django 项目中将一些用户添加到我之前创建的组中,但我知道如何添加到我知道名称并且不是动态的特定组。我希望当我 select 一个特定的组时,已经添加了下一个用户。问题在这里:
g = Group.objects.get(name= 'New')
我应该怎么做才能替换这个并指出我点击的这个组?
views.py:
@login_required
def choose_group(request, pk):
if request.method == "POST":
cvs = get_object_or_404(Cv, pk=pk)
p = Person.objects.create(name=cvs.author)
g = Group.objects.get(name= 'New')
m = Membership.objects.create(person=p, group=g, leader=False)
return redirect( 'proj.views.cv_detail', pk=cvs.pk )
else:
cv = Cv.objects.filter(author = request.user)
cvs = get_object_or_404(Cv, pk=pk)
mem = Membership.objects.all()
context = {
'mem':mem,
'cvs':cvs,
'cv':cv
}
return render(request, 'choose_group.html', context)
models.py:
class Person(models.Model):
name = models.CharField(max_length=128)
def __str__(self): # __unicode__ on Python 2
return self.name
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
def __str__(self): # __unicode__ on Python 2
return self.name
class Membership(models.Model):
person = models.ForeignKey(Person)
leader = models.BooleanField(default=False)
group = models.ForeignKey(Group)
choose_groups.html:
{% block profile %}
<div class="jumbotron">
<h3>Choose a group to add:</h3>
</div>
<ul>
{% for m in mem %}
<form method="POST" class="post-form" >{% csrf_token %}
<li><button type="submit" class="li1"> <b>{{ m.group }}</b></li>
</form>
{% endfor %}
</ul>
{% endblock %}
我在这里看到的主要问题是您没有从 html 发送群组名称 m.group
。
为此,您必须将名称和值添加到 html 中的按钮元素(并且不要忘记关闭按钮元素)
<button type="submit" class="li1" name="group" value="{{m.group}}"> <b>{{ m.group }}</b></button>
然后,您可以使用 request.POST
g = Group.objects.get(name=request.POST['group'])