在django中创建配置文件更新时出错

时间:2017-08-22 19:13:10

标签: python django django-forms

我正在尝试在django应用程序中创建配置文件更新,我在浏览器中收到以下错误

local variable 'form' referenced before assignment

任何帮助将不胜感激。我现在很困惑。以下是我的views.py。感谢

View.py

def update_profile(request):
    if request.method == 'POST':
        form = UpdateProfile(request.POST or None, instance=request.user)
        profile_form = ProfileEditForm(data = request.POST or None,instance=request.user,files=request.FILES)

        if form.is_valid() and profile_form.is_valid():
            form.save()
            profile_form.save()
            return redirect('profiles')
    else:
        form = UpdateProfile(instance=request.user)
        profile_form = ProfileEditForm(instance=request.user)
    context = {
        "form": form,
        "profile_form": profile_form,
    }
    template = 'edit.html'
    return render(request, template, context)

错误跟踪

Traceback:

File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/Users/Olar/Desktop/arbithub/src/profiles/views.py" in update_profile
  49.         if form.is_valid() and profile_form.is_valid():

Exception Type: UnboundLocalError at /profile/edit/
Exception Value: local variable 'form' referenced before assignment

form.py

class UpdateProfile(forms.ModelForm):
    username = forms.CharField(required=True)
    email = forms.EmailField(required=True)
    first_name = forms.CharField(required=False)
    last_name = forms.CharField(required=False)

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'username', 'email']

class ProfileEditForm(forms.ModelForm):

    class Meta:
        model = Profile
        fields = ('date_of_birth', 'photo')

将根据要求提供其他代码。

1 个答案:

答案 0 :(得分:0)

试试这个:

def update_profile(request):
    form = UpdateProfile()
    if request.method == 'POST':
        form = UpdateProfile(data = request.POST or None, instance=request.user)
        profile_form = ProfileEditForm(data = request.POST or None,instance=request.user,files=request.FILES)

        if form.is_valid() and profile_form.is_valid():
            form.save()
            profile_form.save()
            return redirect('profiles')
    else:
        form = UpdateProfile(instance=request.user)
        profile_form = ProfileEditForm(instance=request.user)
    context = {
        "form": form,
        "profile_form": profile_form,
    }
    template = 'edit.html'
    return render(request, template, context)