1

我希望能够通过视图和模板表单将 Django 中的用户添加到用户创建的组中。创建组很容易,我只是无法创建一种将批量用户(如此类email1@email.com, email2@email.com, email203920492@email.com)添加到组的方法。如果用户存在于系统中,他们会收到一条消息加入,如果他们不存在,他们会收到一条消息,邀请他们加入网站和群组。

4

2 回答 2

0

如果我明白你在问什么...

如果您有电子邮件列表,则只需遍历电子邮件列表并获取分配给每封电子邮件的用户:

for email in emails:
    try:
        user = User.objects.get(email=email)
    except User.DoesNotExist:
        # A user doesn't exist with that email address, so send the invitation email

    # The user exists, so send them an email with a link to a view that lets them join    the group

在您看来,当他们访问链接时,您可以将当前登录的用户添加到组中,例如:

request.user.groups.add(group)
于 2012-09-26T20:44:32.357 回答
0

这是一个部分答案——这会从模板中的电子邮件表单返回或创建用户(form model也在下面)。我仍然需要弄清楚如何通过电子邮件向他们发送邀请链接或“接受”群组链接。建议将不胜感激。

@login_required
def community(request):
    places = Community.objects.filter(manager=request.user).order_by('id')
    form = EmailAddForm(request.POST or None)

    if form.is_valid():
        emails = form.cleaned_data['emails'].split(',') # this allows you to enter multiple email addresses in the form and separated by comma
        for email in emails:
            try:
                user = User.objects.get(email=email)
            except User.DoesNotExist:
                user = User.objects.get_or_create(email=email, username=email)
        print user #to check that it's working in console


    return render_to_response('community.html', locals(), context_instance=RequestContext(request))

#forms.py
class EmailAddForm(forms.Form):
    emails = forms.CharField(widget=forms.Textarea)
于 2012-09-26T23:33:35.667 回答