Django表单初始化方法失败

小子

我有一个用户添加表单视图,它要求提供信息并将新用户保存到数据库中。当我尝试保存新的用户记录时,出现以下错误。

TypeError at /user/add/
__init__() missing 1 required positional argument: 'customer'
Request Method: GET
Request URL:    http://localhost:8000/user/add/
Django Version: 3.0.5
Exception Type: TypeError
Exception Value:    
__init__() missing 1 required positional argument: 'customer'
Exception Location: D:\Job Documents\DDS\AuthenticationProject\env\lib\site-packages\django\views\generic\edit.py in get_form, line 33
Python Executable:  D:\Job Documents\DDS\AuthenticationProject\env\Scripts\python.exe
Python Version: 3.9.0
Python Path:    
['D:\\Job Documents\\DDS\\AuthenticationProject\\auth',
 'C:\\Users\\bratca\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\bratca\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\bratca\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\bratca\\AppData\\Local\\Programs\\Python\\Python39',
 'D:\\Job Documents\\DDS\\AuthenticationProject\\env',
 'D:\\Job Documents\\DDS\\AuthenticationProject\\env\\lib\\site-packages']
Server time:    Sun, 15 Aug 2021 08:05:30 +0000

我使用客户对象来区分用户,因此我希望表单知道它是由谁填写的,这样新创建的用户将拥有与创建者相同的客户对象。

此错误并未准确显示代码中我的错误在哪里。相反,它在实际的 django 代码中显示了一些错误,即我正在使用的通用内容。

class AddUserFormView(FormView):
    form_class = TbUserAddForm
    template_name = 'users/add_user_modal.html'
    success_message = "Account has been created! The user is able to log in."

    def post(self, request):
        form = self.form_class(customer=request.user.customer,
                               data=request.POST, files=request.FILES)
        if form.is_valid():
            user_face_img_md5 = Image.open(
                request.FILES['user_face_img_md5'].file)
            user_head_img_md5 = Image.open(
                request.FILES['user_head_img_md5'].file)
            obj = form.save(commit=False)
            response = addUserApi(obj.__dict__,
                                  user_face_img_md5,
                                  user_head_img_md5)
            print(f"Request Response [Add User] --> {response.text}")
            # if response.text['status'] != 45001:
            #     messages.error(f'Something went wrong. {response.text}')
            return redirect('add-user')
        else:
            messages.error(request, form.errors)
            return redirect('add-user')
class TbUserAddForm(CustomUserCreationForm):
    email = forms.EmailField()
    user_head_img_md5 = forms.ImageField(label='Avatar', required=True)
    user_face_img_md5 = forms.ImageField(
        label='Face Recognition Image', required=True)

    def __init__(self, customer, *args, **kwargs):
        super().__init__(*args, **kwargs)
        print(f'USER ADD FORM CUSTOMER -> {customer}')
        self.customer = customer
        if self.customer:
            self.fields['role'].queryset = TbRole.objects.all().filter(
                customer=self.customer)
            self.fields['department'].queryset = TbDepartment.objects.all().filter(
                customer=self.customer)

    class Meta:
        model = TbUser
        fields = [...]

path('add/', views.AddUserFormView.as_view(), name='add-user'),

django 没有保存用户,而是addUserApi()将请求发送到另一台服务器。因此有时会因为 addUserApi 工作而创建用户。但是抛出了同样的错误。

威廉·范·翁塞姆

您应该覆盖.get_form_kwargs(…)方法 [Django-doc]以将数据传递给您在 GET 和 POST 请求中构造的表单:

from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse_lazy

class AddUserFormView(LoginRequiredMixin, SuccessMessageMixin, FormView):
    form_class = TbUserAddForm
    template_name = 'users/add_user_modal.html'
    success_message = 'Account has been created! The user is able to log in.'
    success_url = reverse_lazy('add-user')

    def get_form_kwargs(self):
       formkw = super().get_form_kwargs()
       formkw['customer'] = self.request.user.customer
       return formkw

    def form_valid(self, form):
        user_face_img_md5 = Image.open(self.request.FILES['user_face_img_md5'].file)
        user_head_img_md5 = Image.open(self.request.FILES['user_head_img_md5'].file)
        obj = form.save(commit=False)
        response = addUserApi(
            obj.__dict__,
            user_face_img_md5,
            user_head_img_md5
        )
        print(f'Request Response [Add User] --> {response.text}')
        return super().form_valid(form)

通常不会覆盖基于类的视图post和/或get方法,因为它包含大部分样板代码。可能在这里覆盖.form_valid(…)方法 [Django-doc]来指定在表单有效的情况下应该发生什么就足够了


注意:您可以使用LoginRequiredMixinmixin [Django-doc]将基于类的视图的视图限制为经过身份验证的用户


注意如果表单有效,可以使用SuccessMessageMixinmixin视图添加成功消息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章