2017-09-26 67 views
2

我在HTML頁面中顯示錯誤消息時遇到問題。我正在使用Django的默認UserCreationForm註冊頁面。它有兩個密碼字段 - 一個是原件,一個是確認。當用戶輸入不同的密碼時,我在/ signup /處獲得Value Error,但我希望在HTML頁面中顯示錯誤消息,表明密碼不匹配。我已經閱讀了文檔,並且在代碼中添加了一些相關的行,我不知道自己的錯在哪裏。在DefaultUserCreationForm模板中顯示錯誤消息 - django

這裏是我的views.py:

def adduser(request): 
    if request.method == 'POST': 
     form = UserCreationForm(request.POST) 
     print(request.POST) 

     if(form.is_valid): 
      try: 
       user = employees.objects.get(emp_id=request.POST['username']) 
      except employees.DoesNotExist: 
       user = None 

      print(user) 
      if(user != None): 
       if request.POST['username'] in employees.objects.values_list('manager_id__emp_id',flat=True): 
        g = Group.objects.get(name='Managers') 
        newuser = form.save() 
        newuser.groups.add(g) 
       else: 
        g = Group.objects.get(name='Employees') 
        newuser = form.save() 
        newuser.groups.add(g) 

       return render(request,'login.html',{'form': form}) 
      else: 
       form = UserCreationForm() 
       return render(request,'signup.html', {'form': form, 'msg': 'Enter valid employee id'}) 
    else: 
     form = UserCreationForm() 

     return render(request,'signup.html', {'form': form}) 

這裏是我signup.html:

<body> 
    <div class="container"> 
     <div class="page-header"> 
      <h1>Sign-up Here</h1>  
     </div> 
     {% block body %} 


     <form method="post"> 
      {% csrf_token %} 
      <font color="orange" size="5px"><p> * Enter your Employee id, as username * </p></font> 
      {{ form.as_p }} 
      <font color="red"> {{ msg }} </font><br> 
      <font color="red"> {{ form.password1.errors }} </font><br> 
      <font color="red"> {{ form.password2.errors }} </font><br> 

      <br> 
      <button class="btn btn-success" type="submit"> Go! </button> 
     </form> 

     {% endblock %}  

    </div> 
</body> 
+0

什麼形式的行動? – Vaibhav

+0

您的代碼中還有一些問題/更正可以更正。 –

+0

@Vicmathur,因爲它是一種內置的形式,並不是必需的,它在沒有動作的情況下工作得很好。 –

回答

1

的問題是在這條線:

if(form.is_valid): 

這不是正確的測試方式表單驗證, 改爲使用:

if form.is_valid(): 

此外,您不需要多次聲明表單,它可以單次完成。 像這樣:

def adduser(request): 
    form = UserCreationForm(request.POST or None) 
    if request.method == 'POST': 
     print(request.POST) 
     if form.is_valid(): 
      try: 
       user = employees.objects.get(emp_id=request.POST['username']) 
      except employees.DoesNotExist: 
       user = None 

      print(user) 
      if(user != None): 
       if request.POST['username'] in employees.objects.values_list('manager_id__emp_id',flat=True): 
        g = Group.objects.get(name='Managers') 
        newuser = form.save() 
        newuser.groups.add(g) 
       else: 
        g = Group.objects.get(name='Employees') 
        newuser = form.save() 
        newuser.groups.add(g) 

       return render(request,'login.html',{'form': form}) 
      else: 
       form = UserCreationForm() 
       return render(request,'signup.html', {'form': form, 'msg': 'Enter valid employee id'}) 
    return render(request,'signup.html', {'form': form}) 

並且可以有diffrenmt型誤差修改的,non_field_errors,所以使用成才這樣的:

{% if form.errors %} 
    {% for field in form %} 
     {% for error in field.errors %} 
      <div class="alert alert-danger"> 
       <strong>{{ error|escape }}</strong> 
      </div> 
     {% endfor %} 
    {% endfor %} 
    {% for error in form.non_field_errors %} 
     <div class="alert alert-danger"> 
      <strong>{{ error|escape }}</strong> 
     </div> 
    {% endfor %} 
{% endif %} 
+0

哎呀..愚蠢的錯誤......謝謝:) –