通过视图通过电子邮件将用户添加到组(允许多个逗号分隔符)

时间:2012-09-26 00:42:32

标签: django-templates django-views django-users django-permissions

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

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

以下是部分答案 - 这将从temaplate中的电子邮件表单返回或创建用户(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)