Travel Tips
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Django Crispy Forms TypeError: __init__() got an unexpected keyword argument 'instance'
## 环境 Python3.6 + django2.2
# 错误原因是要使用instance参数 UserChangeInfoForm继承forms.ModelForm 而不是forms.Form
``` class UserChangeInfoForm(forms.Form): class Meta: model = UserProfile fields = ['nick_name', 'sex', 'address', 'phone', 'brithday'] ```
``` def user_change_info(request): user_change_info_form = UserChangeInfoForm(request.POST, instance=request.user) if user_change_info_form.is_valid(): user_change_info_form.save(commit=True) return JsonResponse({"status": 'ok', 'msg': '修改成功'}) else: return JsonResponse({"status": 'fail', 'msg': '修改失败'}) ```
``` class UserChangeInfoForm(forms.ModelForm): class Meta: model = UserProfile fields = ['nick_name', 'sex', 'address', 'phone', 'brithday'] ```
``` def user_change_info(request): user_change_info_form = UserChangeInfoForm(request.POST, instance=request.user) if user_change_info_form.is_valid(): user_change_info_form.save(commit=True) return JsonResponse({"status": 'ok', 'msg': '修改成功'}) else: return JsonResponse({"status": 'fail', 'msg': '修改失败'}) ```
Sed ac lorem felis. Ut in odio lorem. Quisque magna dui, maximus ut commodo sed, vestibulum ac nibh. Aenean a tortor in sem tempus auctor
December 4, 2020 at 3:12 pm
Sed ac lorem felis. Ut in odio lorem. Quisque magna dui, maximus ut commodo sed, vestibulum ac nibh. Aenean a tortor in sem tempus auctor
December 4, 2020 at 3:12 pm
Donec in ullamcorper quam. Aenean vel nibh eu magna gravida fermentum. Praesent eget nisi pulvinar, sollicitudin eros vitae, tristique odio.
December 4, 2020 at 3:12 pm
我是 s enim interduante quis metus. Duis porta ornare nulla ut bibendum
Rosie
6 minutes ago