Django - 更改表格不起作用

时间:2014-02-04 23:12:51

标签: python django django-models django-views

我的Django网站遇到了一些问题。我正在努力建立一个电子商务。我有一个自定义用户模型,我需要一些字段(如地址,电子邮件等)。这是代码:

class UsuarioFunnaticos(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=255, unique=True, db_index=True)
    USERNAME_FIELD = "email"

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)

    nome_completo = models.CharField(max_length=255)
    cep_entrega_cliente = models.CharField(max_length=9)
    rua_entrega_cliente = models.CharField(max_length=100)
    bairro_entrega_cliente = models.CharField(max_length=100)
    numero_entrega_cliente = models.CharField(max_length=10)
    complemento_entrega_cliente = models.CharField(max_length=20, blank=True)

    ACRE = 'AC'
    ALAGOAS = 'AL'

    # Some other choices

    SERGIPE = 'SE'
    TOCANTINS = 'TO' 

    ESTADOS = (
    (ACRE, 'Acre'),
    (ALAGOAS, 'Alagoas'),
    (AMAPA, 'Amapa'),
    (AMAZONAS, 'Amazonas'),
    (BAHIA,'Bahia'),
    (CEARA,'Ceara'),
    (DISTRITO_FEDERAL,'Distrito Federal'),
    (ESPIRITO_SANTO,'Espirito Santo'),
    (GOIAS,'Goias'),
    (MARANHAO,'Maranhao'),
    (MATO_GROSSO,'Mato Grosso'),
    (MATO_GROSSO_DO_SUL,'Mato Grosso do Sul'),
    (MINAS_GERAIS,'Minas Gerais'),
    (PARA,'Para'),
    (PARAIBA,'Paraiba'),
    (PARANA,'Parana'),
    (PERNAMBUCO,'Pernambuco'),
    (PIAUI,'Piaui'),
    (RIO_DE_JANEIRO,'Rio de Janeiro'),
    (RIO_GRANDE_DO_NORTE,'Rio Grande Do Norte'),
    (RIO_GRANDE_DO_SUL,'Rio Grande Do Sul'),
    (RONDONIA,'Rondonia'),
    (RORAIMA,'Roraima'),
    (SANTA_CATARINA,'Santa Catarina'),
    (SAO_PAULO,'Sao Paulo'),
    (SERGIPE,'Sergipe'),
    (TOCANTINS,'Tocantins'),        
    )

    estado_entrega_cliente = models.CharField(max_length=30, choices=ESTADOS)
    cidade_entrega_cliente = models.CharField(max_length=90)
    telefone = models.CharField(max_length=20)

    objects = UsuarioFunnaticosManager()

    def get_full_name(self):
        return self.nome_completo

    def get_short_name(self):
        return self.nome_completo

    def __unicode__(self):
        return self.email

在管理员上,我有一个UsuarioFunnaticosChangeForm,它在Django管理员上完美运行。它的代码是:

class UsuarioFunnaticosChangeForm(forms.ModelForm):
    password = ReadOnlyPasswordHashField()

    class Meta:
        model = UsuarioFunnaticos

    def clean_password(self):
        return self.initial["password"]

现在我正在尝试建立一个用户可以去更改用户信息的地方,如果他们想要更改他们的注册地址,电话等等。我的观点如下:

def alterar_dados(request):
    if request.method == 'POST':        
        form_edicao = UsuarioFunnaticosChangeForm(request.POST)
        if form_edicao.is_valid():
            usuario = UsuarioFunnaticos.objects.get(email=user.request.email)
            usuario.nome_completo = form_edicao.cleaned_data['nome_completo']
            usuario.rua_entrega_cliente = form_edicao.cleaned_data['rua_entrega_cliente']
            usuario.bairro_entrega_cliente = form_edicao.cleaned_data['bairro_entrega_cliente']
            usuario.numero_entrega_cliente = form_edicao.cleaned_data['numero_entrega_cliente']
            usuario.complemento_entrega_cliente = form_edicao.cleaned_data['complemento_entrega_cliente']
            usuario.estado_entrega_cliente = form_edicao.cleaned_data['estado_entrega_cliente']
            usuario.cidade_entrega_cliente = form_edicao.cleaned_data['cidade_entrega_cliente']
            usuario.telefone = form_edicao.cleaned_data['telefone']
            usuario.save()
            return HttpResponseRedirect('/conta')        
    else:        
        form_edicao = UsuarioFunnaticosChangeForm()        
    return render_to_response('conta_cliente/alterar_dados.html', { 'form_edicao': form_edicao }, context_instance = RequestContext(request))

我也尝试过使用form_edicao.save(),但两者都返回了以下错误:

KeyError at /conta/alterardados/

'password'

我不希望我的用户再次输入他们的密码来更改他们的用户信息,因为如果他们已经登录,他们只能到达网站的这一部分。有人可以提供一些帮助吗?

1 个答案:

答案 0 :(得分:1)

那么,在实例化表单时,您应该将现有的用户对象作为instance参数传递。

但我不明白所有这些关于密码的问题。如果您不希望密码字段位于表单上,只需将其添加到表单的Meta类中的exclude列表中。