创建用户后,上传带有用户详细信息的csv文件并发送reset-password-email Django

时间:2018-09-21 20:39:23

标签: python django

我在触发系统向用户发送密码重置电子邮件时遇到问题。我将上传一个包含客户详细信息的csv文件(其中包含诸如FN,LN,电子邮件地址之类的详细信息),一旦上传成功,系统就必须向客户发送密码重置电子邮件。

现在,当我尝试上传csv 'UploadFileForm'对象没有属性'save'

时,出现以下错误

1)在导入/上传CSV文件时,必须创建用户,并且用户会收到密码重置电子邮件以更改密码----这是由系统完成的。

注意-我正在使用Django 2.0.8,我也尝试了此链接Trigger Password rest

view.py

def validate_csv(value):
    if not value.name.endswith('.csv'):
        raise ValidationError('Invalid file type')

def handle_uploaded_file(request):
          csvf = StringIO(request.FILES['file'].read().decode())
          reader = csv.reader(csvf, delimiter=',')
          reader.__next__();
          count = 0
          for row in reader:
                vu = ValidUser(email = row[1],first_name = row[2],last_name = row[3])
                if vu is not None:
                    vu.save()
                    count = count + 1
          return count

@login_required
def registerusers(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            file_name = request.FILES['file']
            validate_csv(file_name)
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            subject = 'Enter Password for your Account'
            message = render_to_string('account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': default_token_generator.make_token(user),
            })
            user.email_user(subject, message)
           # return redirect('account_activation_sent')
           #  return redirect('dashboard')
            value = handle_uploaded_file(request)
            if value > 0:
                messages.success(request, str(value)+' users added successfully')
    else:
        form = UploadFileForm()
    return render(request, 'account/registerusers.html')

forms.py

from django import forms
from django.contrib.auth.models import User

class UploadFileForm(forms.Form):
    file = forms.FileField(label=" Choose the CSV file")
    class Meta:
        model = User

models.py

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

class ValidUser(models.Model):
    #user = models.OneToOneField(User, on_delete=models.CASCADE)
    email = models.EmailField(blank=True, null=None)
    first_name = models.CharField(max_length=50, default=None)
    last_name = models.CharField(max_length=50, default=None)
    is_active = models.BooleanField(_('active'), default =False)

错误

[21/Sep/2018 15:36:30] "GET /users/ HTTP/1.1" 200 4139
[21/Sep/2018 15:36:31] "GET /registerusers/ HTTP/1.1" 200 4158
Internal Server Error: /registerusers/
Traceback (most recent call last):
  File "C:\Users\nik\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\nik\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\nik\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\nik\Anaconda3\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\Users\nik\Desktop\Version - 7\project\account\views.py", line 91, in registerusers
    user = form.save(commit=False)
AttributeError: 'UploadFileForm' object has no attribute 'save'

0 个答案:

没有答案