2012-10-16 23 views
-1

我使用Django 1.4與Python 2.7和Ubunutu 12.04。強制一個表單域在Django中填充

我有一個表單將更新用戶的配置文件。表單中的最後一項是密碼。我用現有的用戶數據預填充表單。密碼字段沒有預先填充 - 這很好。

問題是,當我「保存」數據時,它會將密碼覆蓋爲空或空字段(我無法分辨出哪個)。壞。

我能做些什麼來防止這種情況發生?

我試圖讓它必填字段(forms.py):

password = forms.CharField(widget = forms.PasswordInput(), required = True) 

沒有工作。

我試圖檢查密碼不None更新它(views.py)前:

if (request.POST.get('password') is not None): 
    user.set_password(request.POST.get('password')) 

沒有工作。

空表格值是否會返回爲None?如果不是,它會返回什麼狀態,如何檢查它是否爲空?

編輯1: 我更新了一個我的views來檢查驗證 - 也許我做錯了嗎?

@login_required 
def profile(request): 
    """ 
    .. function:: profile() 

     Provide the profile page, where it can be updated 

     :param request: Django Request object 
    """ 
    if request.user.is_authenticated(): 
     user = User.objects.get(username = request.user.username) 
     user_dict = createUserProfileDict(user) 
     form = ProfileForm(initial = user_dict); 
     data = { 'user' : request.user } 
     data.update({ 'form' : form }) 
     data.update(csrf(request)) 

     if form.is_valid(): 
      return render_to_response("profile.html", data) 

現在,我收到以下錯誤:

The view rsb.views.profile didn't return an HttpResponse object. 

所以,看來我的表格是不是有效?我怎樣才能找出原因?

這裏是update_profile view

@login_required 
def update_profile(request): 
    """ 
    .. function:: profile() 

     provide the profile page 

     :param request: Django Request object 
    """ 
    if request.user.is_authenticated(): 
     user = User.objects.get(username = request.user) 
     user.first_name = request.POST.get('first_name') 
     user.last_name = request.POST.get('last_name') 
     user.email = request.POST.get('email') 
     if (request.POST.get('password') is not None): 
      user.set_password(request.POST.get('password')) 
     user.save() 

     # Update the additional user information tied to the user 
     user_info = UserProfile.objects.get(user_id = user.id) 
     user_info.company_name = request.POST.get('company_name') 
     user_info.client_type = request.POST.get('client_type') 
     user_info.address1 = request.POST.get('address1') 
     user_info.address2 = request.POST.get('address2') 
     user_info.city = request.POST.get('city') 
     user_info.state = request.POST.get('state') 
     user_info.country = request.POST.get('country') 
     user_info.zip_code = request.POST.get('zip_code') 
     user_info.phone_number = request.POST.get('phone_number') 
     user_info.save() 

    return profile(request) 
+0

你檢查提交的表單是有效的? – Rohan

+0

我不知道如何做到這一點。 'if(request.POST.get('password')。is_valid())'?我閱讀了關於這個的Django文檔,但是一如既往,他們沒有實際的例子。 :( – Rico

+0

is_valid指的是完整的表單對象,而不是單個字段 –

回答

1

首先,記住要控制,如果你的形式「is_valid()」

要theck如果表單已提交具有空值與否,使用

MyForm.has_changed() 

太糟糕了,這不是一個文件的功能:(

如果你想有一個默認的密碼,我建議你檢查字段是有效的,那麼使用類似

''.join([choice(string.letters + string.digits) for i in range(7)]) 

來爲用戶生成新密碼(範圍(7)是你想要的長度)。然後用一個選擇的方法(參見:向用戶發送一封電子郵件,他的臨時密碼)

編輯基於新的上下文:

從Django文檔:

If a Field has required=False and you pass clean() an empty value, 
then clean() will return a normalized empty value 
rather than raising ValidationError.  
For CharField, this will be a Unicode empty string. 
For other Field classes, it might be None. (This varies from field to field.) 

就是這樣,你的密碼字段應該要求=假,這樣你就可以在你的觀點對待,作爲一個空字符串

然後,你可以這樣做:

if input_password != '' and input_password != saved_password: 
    saved_password = input_password 

這只是僞代碼,但它應該給你一個明確的概念

+0

你能澄清一下在哪裏'MyForm.has_changed()'會去?在'view'中?我不想生成密碼 - 我只是想讓它如果不輸入任何內容作爲密碼,那麼它不會更新密碼 – Rico

+0

是的,你應該在你的pview中處理這些東西(在你發佈表單之後),我現在看到你的問題了,不明白,我會編輯我的答案,然後你只需要檢查新的密碼一個。如果他們不匹配,並且該字段不是None或不爲空,則更新:) –

+0

我看到...有點反直觀,但我明白了。謝謝。 – Rico