django Keyerror密码

问题描述 投票:1回答:1

我正在尝试创建一个编辑用户个人资料功能,允许用户编辑他们的个人资料信息。当我点击表单中的提交时,它会在相册/个人资料/编辑中显示为keyerror密码。

Error Screen Shot

views.朋友:

from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from django.contrib.auth.forms import PasswordChangeForm
from django.views.generic import View
from albums.models import Album, Song

from .forms import Userforms, EditProfileForm

class Profile_edit(View):
    form_class = EditProfileForm
    template_name = 'accounts/edit_profile.html'

    def get(self, request):
        form = self.form_class(None)
        return render(request, self.template_name, {'form': form})

    def post(self, request):
        form = self.form_class(request.POST)

        if form.is_valid():
            form.save()
            return redirect('profile')
        return render(request, self.template_name, {'form':form})

forms.朋友:

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

class EditProfileForm(UserChangeForm):

    class Meta:
        model = User
        fields = {'email', 'first_name', 'last_name', 'password'}

URLs.朋友

url(r'^profile/edit/$', views.Profile_edit.as_view(), name='Profile_edit'),

edit_profile.html:

{% extends 'albums/base.html' %}

{% block title %}Sign Up{% endblock %}

{% block albums_active %}active{% endblock %}

{% block body %}

<div class="container-fluid">

<div class="row">

<div class="col-sm-12 col-md-7">

<div class="panel panel-default">

<div class="panel-body">

{% extends 'albums/base.html' %}

{% block title %}Sign Up{% endblock %}

{% block albums_active %}active{% endblock %}

{% block body %}

<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">

  {% csrf_token %}

  {% include 'albums/form-template.html' %}

<button type="submit" class="btn btn-success">Submit</button>

</form>

{% endblock %}
django python-3.x keyerror
1个回答
0
投票

初始化时,您需要将用户实例传递给表单,否则表单不知道您正在编辑哪个用户。

def get(self, request):
    form = self.form_class(instance=request.user)

def post(self, request):
    form = self.form_class(request.POST, instance=request.user)
© www.soinside.com 2019 - 2024. All rights reserved.