(Django)无法更新自定义配置文件

时间:2018-03-06 20:33:33

标签: python django django-models django-forms django-views

所以我试图在扩展更新中更新用户的信息,但是在我添加其余内容之前它已经有效,但是现在它无法正常工作我还没试过回去。

所以它确实发了帖子但是无法验证它。

HTML

<form action="{% url 'userprofileupdate' logged_in_user.pk %}" method="POST">
{% csrf_token %}
https://pastebin.com/vMw6AEai (only Pastebin because of its a lot of code)

Views.py

@login_required
def userprofileupdate(request, user_pk):
    if request.method == 'POST':
        form = UpdateProfileForm(request.POST, instance=request.user)
        if form.is_valid():
            user = form.save()
            user.refresh_from_db()  # load the profile instance created by the signal
            user.extendeduser.nickname = form.cleaned_data.get('username')
            user.extendeduser.postal_code = form.cleaned_data.get('postal_code')
            user.extendeduser.phone_number = form.cleaned_data.get('phone_number')
            user.extendeduser.emergency_number = form.cleaned_data.get('emergency_number')
            user.extendeduser.birthdate = form.cleaned_data.get('birthdate')
            user.extendeduser.languages = form.cleaned_data.get('languages')
            user.extendeduser.drivers_licence = form.cleaned_data.get('drivers_licence')
            user.extendeduser.tshirt = form.cleaned_data.get('tshirt')
            user.extendeduser.special_considerations = form.cleaned_data.get('special_considerations')
            user.save()
            messages.success(request, "Your profile has been updated!")
            return redirect('usersettings', user_pk=request.user.pk)

    messages.error(request, 'Error: please update your settings if you want to update them')
    return redirect('usersettings', user_pk=request.user.pk)

forms.py

class UpdateProfileForm(forms.ModelForm):
    postal_code = forms.CharField(max_length=10, required=True)
    phone_number = forms.CharField(max_length=16, required=True)
    emergency_number = forms.CharField(max_length=16, required=True)
    birthdate = forms.DateField(required=False)
    languages = forms.MultipleChoiceField(required=False)
    drivers_licence = forms.MultipleChoiceField(required=False)
    tshirt = forms.ChoiceField(required=False)
    special_considerations = forms.CharField(required=False)
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'birthdate', 'phone_number', 'emergency_number', 'postal_code', 'languages', 'drivers_licence', 'tshirt', 'special_considerations',)

3 个答案:

答案 0 :(得分:0)

您没有保存扩展用户对象。

user.extendeduser.save()

答案 1 :(得分:0)

1 - 首先运行./manage.py shell 2 - 在shell中运行所有update user命令以验证错误是否存在。 3 - 如果错误没有发布./manage.py runserver的错误日志,那么我将更好地准确地回答你的问题。

答案 2 :(得分:0)

所以我错过的是我在模型中使用了许多非常多的外键,并且没有看到那里有一个名为ModelChoiceField和ModelMultipleChoiceField的字段就是它没有用的情况。所以我基本上改变了我的形式,然后它起作用了

Form.py

from django.forms import ModelChoiceField, ModelMultipleChoiceField    
class TshirtChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.description

class DriversLicenceCategoriesMultipleChoiceField(ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return obj.category

class LanguageMultipleChoiceField(ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return obj.name

class UpdateProfileForm(forms.ModelForm):
    birthdate = forms.DateField(input_formats=['%d-%m-%Y'],required=False)
    phone_number = forms.CharField(max_length=16, required=True)
    emergency_number = forms.CharField(max_length=16, required=False)
    postal_code = forms.CharField(max_length=10, required=True)
    languages = LanguageMultipleChoiceField(queryset=Language.objects.all(),required=False)
    drivers_licence = DriversLicenceCategoriesMultipleChoiceField(queryset=DriversLicenceCategories.objects.all(),required=False)
    tshirt = TshirtChoiceField(queryset=TShirt.objects.all(),required=False)
    special_considerations = forms.CharField(required=False)
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'birthdate', 'phone_number', 'emergency_number', 'postal_code', 'languages', 'drivers_licence', 'tshirt', 'special_considerations',)