1

我已經爲我的Django項目創建了登錄表單,並且出現錯誤消息。 這是我在views.py功能:Django登錄表單錯誤信息

def user_login(request): 
if request.method == 'POST': 
    username = request.POST.get('username') 
    password = request.POST.get('password') 

    user = authenticate(username=username, password=password) 
    if user: 
     if user.is_active: 
      login(request, user) 
      return HttpResponseRedirect('/friends_plans/users/') 
     else: 
      return HttpResponse("Your account is disabled") 
    else: 
     print "Invalid login details: {0}, {1}".format(username, password) 
     return HttpResponse("Invalid login details supplied.") 
else: 
    return render(request, 'friends_plans/index.html', {}) 

錯誤消息(「無效的登錄信息附帶的」)將出現一個新的空白頁面上。如果用戶名或密碼在相同的登錄頁面(index.html)上不正確,請告訴我如何顯示此消息?

這是我的模板的index.html:

{% load staticfiles %} 
<html > 
<head > 
    <title> Friends' Plans </title> 
    <meta charset ="utf -8" /> 
    <link rel="stylesheet" href="{% static 'css/friends_plans.css' %}"> 
</head > 
<body > 
    <div id ="container"> 
     <div id ="header"> 
      <ul id ="menu"> 
       <span><a id="firstbutton" href ="" >Friends' Plans</a> </span> 
       <span><a id="helpbutton" href ="" >HELP</a></span> 
      </ul> 
     </div> 
     <div id ="left"> 
      <form id="login_form" method="post" action=""> 
       {% csrf_token %} 

       Username: <input type ="text" name ="username" value="" size="50" /> <br /> 
       Password: <input type ="password" name ="password" value="" size="50"/> <br /> 
       <input type ="submit" value="submit" /> 
      </form> 
      {% if user.is_authenticated %} 
      <a href="/friends_plans/logout/">Logout</a> 
      {% else %} 
      <a href="/friends_plans/register/">Register here</a><br /> 
      {% endif %} 
     </div> 
     <div id ="right"> 
      <h1 id="welcome">Welcome to Friends' Plans</h1> 
      <img class="cat" src={% static 'images/cat4.jpg' %} /> 
      <img class="cat" src={% static 'images/cat2.jpg' %} /> 
      <img class="cat" src={% static 'images/cat3.jpg' %} /> 
      <img class="cat" src={% static 'images/cat6.jpg' %} /> 
      <img class="cat" src={% static 'images/cat5.jpg' %} /> 
      <img class="cat" src={% static 'images/cat1.jpg' %} /> 
     </div> 
     <div id ="footer"> Copyright </div> 
    </div> 
</body> 
</html> 

我有一個想法,以設置變量error=False並且如果error=True如果form.is_valid(改變)爲假,並在模板寫{% if error %} ,但csrf_token存在錯誤,但模板中有{% csrf_token %}

回答

3

在views.py:

def login_process(request): 
    try: 
     user = authenticate(username = request.POST['username'], 
      password = request.POST['password']) 
    except KeyError: 
     return render(request, 'friends_plans/index.html',{ 
      'login_message' : 'Fill in all fields',}) 
    if user is not None: 
     #'User' and 'Pass' right 
     if user.is_active: 
      login(request, user) 
     else: 
      return render(request, 'friends_plans/index.html',{ 
       'login_message' : 'The user has been removed',}) 
    else: 
     return render(request, 'friends_plans/index.html',{ 
      'login_message' : 'Enter the username and password correctly',}) 
    return HttpResponseRedirect('/friends_plans/users/') 

index.html中:

{% load staticfiles %} 
<html > 
<head > 
    <title> Friends' Plans </title> 
    <meta charset ="utf -8" /> 
    <link rel="stylesheet" href="{% static 'css/friends_plans.css' %}"> 
</head > 
<body > 
    <div id ="container"> 
     <div id ="header"> 
      <ul id ="menu"> 
       <span><a id="firstbutton" href ="" >Friends' Plans</a> </span> 
       <span><a id="helpbutton" href ="" >HELP</a></span> 
      </ul> 
     </div> 
     <div id ="left"> 
      <form id="login_form" method="post" action=""> 
       <div class="add_your_styles"> 
        {{ login_message }} 
       </div> 
       {% csrf_token %} 
       Username: <input type ="text" name ="username" value="" size="50" /> <br /> 
       Password: <input type ="password" name ="password" value="" size="50"/> <br /> 
       <input type ="submit" value="submit" /> 
      </form> 
      {% if user.is_authenticated %} 
      <a href="/friends_plans/logout/">Logout</a> 
      {% else %} 
      <a href="/friends_plans/register/">Register here</a><br /> 
      {% endif %} 
     </div> 
     <div id ="right"> 
      <h1 id="welcome">Welcome to Friends' Plans</h1> 
      <img class="cat" src={% static 'images/cat4.jpg' %} /> 
      <img class="cat" src={% static 'images/cat2.jpg' %} /> 
      <img class="cat" src={% static 'images/cat3.jpg' %} /> 
      <img class="cat" src={% static 'images/cat6.jpg' %} /> 
      <img class="cat" src={% static 'images/cat5.jpg' %} /> 
      <img class="cat" src={% static 'images/cat1.jpg' %} /> 
     </div> 
     <div id ="footer"> Copyright </div> 
    </div> 
</body> 
</html> 

您可以將{{登錄消息}}任何地方。這只是一個文本字符串。

+0

非常感謝您的回答!但是當我嘗試設置一個變量'error = False'時,我遇到了同樣的錯誤:Forbidden(403)SCRF驗證失敗。請求中止。你有什麼想法在SCRF模板中有什麼問題嗎? –

+0

謝謝!像魅力一樣工作! –

0

對於這樣的事情,您可以使用Django的消息框架。 在你的進口,增加線

from django.contrib import messages 

這將使信息框架提供給你,現在你要添加的錯誤信息來完成這個失敗的登錄請求,因此,所有你需要做的就是

messages.error(request, 'Invalid login credentials'). 

接下來,失敗的嘗試呈現相同的登錄頁面。在您的登錄模板中,您將有可用的變量消息,這基本上是您從視圖發送的消息的列表。您可以遍歷它並在同一頁面上顯示它。

+0

非常感謝!我使用了消息。但是,正如你寫的那樣,這是一個消息列表,例如,如果我輸入了無效的登錄名/密碼兩次,那麼有兩條消息,但必須只有一條消息。你能告訴我如何做到這一點?在模板中,我寫了'{%for message in messages%} {{message}} {%endfor%}' –

+0

請參閱它可能應該如何,請求登錄表單,放入憑據,在視圖中進行身份驗證,如果有任何錯誤,您將它們放入消息並重定向到相同的視圖,即您的登錄頁面,這樣您將在表單中顯示一條消息,因爲在身份驗證失敗時,您將被重定向到相同的登錄帶消息的頁面。 –