考虑使用textarea输入来输入电子邮件。 该视图具有forloop:对于每个电子邮件,视图都会尝试创建用户。然后,如果用户被创建,我将它附加到我的数组“newUers”,如果用户已经存在,我将它附加到“alreadyUsers”数组。
当循环结束时,我想用这些数组渲染视图。但它不起作用...... 用户已正确创建,但视图不呈现模板。
有什么想法吗?
invitations.html :提交时带有ajax调用的textarea
<div id="invitations">
<label>Enter your clients email</label>
<textarea v-model="invitations" class="textarea" id="invitations" name="invitations"></textarea>
</div>
<div>
<input id="send" type="submit" value="Save" @click="submit()">
</div>
user_invitation.js :ajax调用(基于Vue)
var vue = new Vue({
el: '#invitations',
data: {
invitations: '',
},
methods: {
submit: function () {
var self = this;
$.ajax({
type: 'POST',
url: '/accounts/invitations/create/',
data: {
invitations: this.invitations,
csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val(),
},
context: this,
success: function () {
console.log("ajax success");
},
error: function () {
console.log("ajax error");
}
})
}
}
});
views.py
def InvitationCreateView(request):
newUsers = []
alreadyUsers = []
for client in request.POST['invitations'].split('\n'):
try:
# Get informations for creating the User
user = User.objects.create(
username=client,
email=client,
)
user.groups.add(Group.objects.get(name='Clients'))
user.save()
newUsers.append(client)
# Check if user already exists
except IntegrityError:
alreadyUsers.append(client)
return render(request, 'accounts/agent_invitations_confirmation.html',
{"newUsers": newUsers, "alreadyUsers": alreadyUsers})