为什么不干净()?

时间:2014-04-15 15:40:04

标签: python django

请帮助解决问题。在页面上是一种形式:

from django import forms
from userprofile.models import UserProfile
from django.contrib.auth.models import User
from django.forms import ModelForm
from django.conf import settings
import os

class ChangePasswordForm (forms.Form):
    password1 = forms.CharField (widget = forms.PasswordInput ())
    password2 = forms.CharField (widget = forms.PasswordInput ())

def clean (self):
    cleaned_data = self.cleaned_data
    password1 = cleaned_data.get ("password1")
    password2 = cleaned_data.get ("password2")
        
    with open (os.path.join (settings.BASE_DIR, "debug_local.txt"), "wb") as f:
        f.write (bytes (password1, 'UTF- 8') )

    if password1! = password2:
        raise forms.ValudationError ("Passwords must be same")
    else:
        User.set_password (password1)

views.py:

from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.template import loader, RequestContext
from django.core.context_processors import csrf
from userprofile.forms import UserProfileForm
from django.contrib.auth.decorators import login_required
from django.conf import settings
from userprofile.forms import ChangePasswordForm


def custom_proc (request):
    return {
        'user_profile_show': True,
    }

@ login_required
def password_page (request):
    t = loader.get_template ('password_page.html')
    c = RequestContext (request, {
        'form': ChangePasswordForm,
    }, [Custom_proc])
    return HttpResponse (t.render (c))

password_page.py:

{% Extends "base.html"%}

{% Block title%} User profile - pass {% endblock%}

{% Block content%}
<h2> pass page </ h2>

{% For field in form%}
{{Field.error}}
{% Endfor%}

<form action="/userprofile/" method="POST"> {% csrf_token%}
{{Form.as_p}}

<input type="submit" value="send" />
</ form>
{% Endblock%}

问题是没有工作的功能干净()。我正在尝试将输出调试到文件debug_local.txt值password1。但是这个文件不是写的

2 个答案:

答案 0 :(得分:2)

您对清洁功能的缩进是错误的。如果它以这种方式缩进,它只是一个函数,而不是你的类的方法。

class ChangePasswordForm (forms.Form):
    password1 = forms.CharField (widget = forms.PasswordInput ())
    password2 = forms.CharField (widget = forms.PasswordInput ())

    def clean (self):
        cleaned_data = self.cleaned_data
        password1 = cleaned_data.get ("password1")
        password2 = cleaned_data.get ("password2")

        with open (os.path.join (settings.BASE_DIR, "debug_local.txt"), "wb") as f:
            f.write (bytes (password1, 'UTF- 8') )

        if password1! = password2:
            raise forms.ValudationError ("Passwords must be same")
        else:
            User.set_password (password1)

答案 1 :(得分:2)

首先你需要小心你的空白。这部分是您的代码风格,部分复制和粘贴到SO而不重新格式化。无论如何,请查看pep8指南。它们让每个人的生活更轻松。

在您的视图中,您永远不会将POST数据传递给表单,因此永远不会验证用户输入。

完成后,您可以验证表单。

最后,一旦表单有效,您可以设置用户密码

免责声明我写了一个基于功能的视图已经有一段时间了。

view.py
@login_required
def password_page(request):
    if requesst.method == 'POST':
        form = ChangePasswordForm(request.POST)
        if form.is_valid():
            request.user.set_password(form.cleaned_data.get('password'))
    else
        form = ChangePasswordForm()

    t = loader.get_template('password_page.html')
    c = RequestContext (request, {
        'form': ,
    }, [Custom_proc])
    return HttpResponse (t.render (c))

我已删除表单中更改的密码。就个人而言,我更喜欢这个。

此外,您的clean()方法必须返回cleaned_data

forms.py
def clean (self):
    cleaned_data = self.cleaned_data
    password1 = cleaned_data.get("password1")
    password2 = cleaned_data.get("password2")

    # DEBUGGING
    # Uncomment this, run your view and head to the
    # console to see what is happening
    # import pdb; pdb.set_trace()

    if password1 != password2:
        raise forms.ValudationError ("Passwords must be same")

    return cleaned_data
相关问题