邀请用户

时间:2017-09-24 10:26:19

标签: django django-models django-forms

我无法找出错误。在我的应用程序中,我正在创建一个设置为非活动状态的用户,然后向这些用户发送加入我的应用程序的邮件邀请。我将其设置为能够发送多个邀请,但它只在X邀请中发送一个。因此,如果我邀请了5个用户,则仅发送邮件并创建1个用户而不是5个。我猜我的循环有问题但无法找到... 这是我的代码

views.py:

def TeamRegister2(request):
    InviteFormSet = formset_factory(InviteForm2)

    if request.method == 'POST':
        formset = InviteFormSet(request.POST, prefix = 'pfix')

        if(formset.is_valid()):
            for i in formset:
                mail = i.cleaned_data['Email']
                user = MyUser(email = mail)
                password = MyUser.objects.make_random_password()
                user.set_password(password)
                user.is_active = False
                user.is_employee = True
                user.save()
                u1 = user.id
                a1 = MyUser.objects.get(email = request.user.email)
                a2 = Project.objects.filter(project_hr_admin = a1)
                a3 = a2.latest('id')
                a4 = a3.team_id
                a4.members.add(u1)

                current_site = get_current_site(request)
                message = render_to_string('acc_active_email.html', {
                    'user':user,
                    'domain':current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                    'token': account_activation_token.make_token(user),
                })
                mail_subject = 'You have been invited to SoftScores.com please sign in to get access to the app'
                to_email = user.email
                email = EmailMessage(mail_subject, message, to=[to_email])
                email.send()
            return HttpResponse('An email have been sent to each Team member asking them to join in')
        else:
            print("The entered form is not valid")

    else:
        formset = InviteFormSet(prefix= 'pfix')
    return render(request,'team_register.html', {'formset':formset})

form.py:

class InviteForm2(forms.Form):
    """
    Form for member email invite
    """
    Email = forms.EmailField(
                    widget=forms.EmailInput(attrs={
                        'placeholder': "Member's mail",
                    }),
                    required=False)

team_register.html:

{% extends 'base.html' %}
{% load static %}

{% block body %}

<div class="container">
  <div class="jumbotron">
    <div class="title">
      <h2>Invite your Team members</h2>
      <h3>You can choose up to 7 team members</h3>
    </div>
  </div>
   <div class="form-style">
     <form method="post" class="form-inline">
       {% csrf_token %}

       {{ formset.management_form }}

       {% for form in formset %}
           <div class="link-formset">

                 {{ form.label_tag }}  {{ form }}
                 {% if field.help_text %}
                   <small style="display: none">{{ field.help_text }}</small>
                 {% endif %}
                 {% for error in field.errors %}
                   <p style="color: red">{{ error }}</p>
                 {% endfor %}

            </div>

        {% endfor %}

       <div>
         <input type="submit" value="Send Invitations" class="btn btn-primary"/>
       </div>

    </form>

</div>
</div>
         <!-- Include formset plugin - including jQuery dependency -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'js/jquery.formset.js' %}"></script>
<script>
    $('.link-formset').formset({
        addText: 'add member',
        deleteText: 'remove'
             });
</script>


{% endblock %}

0 个答案:

没有答案