2011-08-10 135 views
0

我有一個窗體驗證程序,當它失敗時,錯誤消息不顯示。任何想法我失蹤?Django窗體錯誤沒有顯示

views.py:

def setup_onesheet(request): 
    # if the form has been submitted 
    if request.method == 'POST': 
     if 'unverified_username' in request.POST: 
      form = OnesheetURL(request.POST) 
      if form.is_valid(): 
       ... 

    form = OnesheetURL() 
    variables = RequestContext(request, { 
     'error_message': error_message, 
     'form' : form, 
    }); 
    return render_to_response('onesheet_setup/setup_new_onesheet.html', variables) 

forms.py:

class OnesheetURL(forms.Form): 
    unverified_username = forms.CharField(label='http://onesheet.com/', max_length=75, validators=[validate_slug]) 

模板:

<form method="post" action="/setup/new/" id="verify-identity" class="full"> 
    {% csrf_token %} 
    <ul> 
     <li> 
      {{ form.unverified_username.errors }} 
      {{ form.unverified_username.label_tag }} 
      {{ form.unverified_username }} 
      <p>Onesheet URL can only contain letters, numbers, underscores (_) or hyphens (-).</p> 
     </li> 
    </ul> 
    <input type="submit"> 
</form> 

回答

1

嘗試:

def setup_onesheet(request): 
    # if the form has been submitted 
    if request.method == 'POST': 
     if 'unverified_username' in request.POST: 
      form = OnesheetURL(request.POST) 
      if form.is_valid(): 
       ... 
    else: 
     form = OnesheetURL() 

    variables = RequestContext(request, { 
     'error_message': error_message, 
     'form' : form, 
    }); 
    return render_to_response('onesheet_setup/setup_new_onesheet.html', variables) 
+0

謝謝。 Facepalm – Brenden

+3

Dude - 別擔心。我facepalm定期:) – Brandon