2014-03-25 76 views
0

我無法從我的模板傳遞值來查看。基本上,我爲用戶註冊創建了一個表單,並希望將模板中的值傳遞給查看,以便我可以構造一個User對象以添加到我的數據庫中。但不知何故,當我加入name={{user_form.username}}時,我的「輸入」不起作用。另外,當我選擇「借款人」,「libarrian」和「店員」時,我想分別傳遞值0,1,2,我該如何執行此操作?Django將值從模板傳遞到視圖

以下是我的代碼。

 <form id="user_form" method="post" action="/sign_up/" class="form-signin" role="form"> 
      {% csrf_token %} 
     <h2 class="form-signin-heading">Signup </h2> 

      {% if error%} 

     <div class="error"> Your registration has been unsuccessfull </div> 
     {% endif %} 

     <input type="text" class="form-control" name="username" value="{{user_form.username}}" placeholder="Username" required autofocus> 
     <input type="password" class="form-control" name="password1" placeholder="Password" value="" required> 
     <input type="password" class="form-control" name="password2" placeholder="Retype password" value="" required>  


     <select class="form-control"> 
    <option value="2">Librarian</option> 
    <option value="0">Borrower</option> 
    <option value="1">Clerk</option> 
</select> 

     <button class="btn btn-lg btn-primary btn-block" type="submit">Sign Up</button> 

     </form> 

forms.py

class UserForm(forms.Form): 
    username = forms.CharField(max_length=30) 
    password1 = forms.CharField(widget=forms.PasswordInput()) 
    password2 = forms.CharField(widget=forms.PasswordInput()) 

views.py

def sign_up(request): 
    registered = False; 
    error = False; 
    if request.method == 'POST': 
     user_form = UserForm(request.POST) 
     if user_form.is_valid(): 
      registered = True 
      username = user_form.cleaned_data['username'] 
      password = user_form.cleaned_data['password'] 
      user = User.objects.create_user(username, None, password) 
      user.save() 
     else: 
      error = True; 
    else: 
     user_form = UserForm() 

    return render(request, 'books/sign_up.html', 
        {'user_form':user_form, 
        'registered':registered, 'error':error}) 
+0

你能提供模板渲染線 - 你如何將表單對象傳遞給模板? – johnbarr

+0

我從來沒有聽說過類型爲「username」的任何「輸入」控件..這是一種類型還是您將其命名爲這樣? – anuragal

回答

1

以下表格應該工作。

<form id="user_form" method="post" action="/sign_up/" class="form-signin" role="form"> 
    {% csrf_token %} 
    <h2 class="form-signin-heading">Signup </h2> 
    <input type="text" class="form-control" name="username" value="{{user_form.username}}" placeholder="Username" required autofocus> 
    <input type="password" class="form-control" name="password1" placeholder="Password" required> 
    <input type="password" class="form-control" name="password2" placeholder="Retype password" required>  
    <select class="form-control"> 
     <option value="1">Librarian</option> 
     <option value="0">Borrower</option> 
     <option value="2">Clerk</option> 
    </select> 
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign Up</button> 
</form> 

變化我做的 - 用戶名和密碼的文本框

  • 變化選擇控件的類型用戶名文本框
  • 附加價值屬性的

    1. 更改名稱
  • +0

    爲什麼要更改爲name =「username」將值傳遞給user_form.username? – user2649814

    +0

    值應該傳遞給'value'屬性。現在編輯我的答案。 – anuragal

    +0

    我不太理解你的評論。你從未提到過上面的價值屬性。 – user2649814

    相關問題