2017-02-28 49 views
1

我已經發布我的網站與pythonanywhere,裏面有一個簡單的用戶登錄,我創建了一個用戶,當我用我自己的計算機驗證它的工作原理,但是當我與另一臺機器驗證我得到這個錯誤:爲什麼我無法從我的Django網站上的其他計算機進行身份驗證?

禁止(403) - CSRF驗證失敗。請求中止。

我的網站發佈在域名上,其他一切正常。可能是什麼問題?

因爲我可以用我自己的計算機登錄,所有設置都正確,具有正確的中間件,上下文和csrf保護。

更新,代碼:

MIDDLEWARE_CLASSES = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 

login.py

def login(request): 
    context = {} 
    if request.method == "POST": 
     username = request.POST['username'] 
     password = request.POST['password'] 
     user = authenticate(username=username, password=password) 
     if user is not None: 
      if user.is_active: 
       auth_login(request, user) 
       return redirect('login') 
      else: 
       context['error'] = 'L\'utilisateur a été désactivé' 
     else: 
      context['error'] = 'Mauvais nom d\'utilisateur ou mot de passe' 

    return render(request, 'user.html', context) 

user.html

<form method="post" action="{% url 'login' %}"> 
    {% csrf_token %} 
    <p>Nom :<input type="text" class="form-control" name="username"></p> 
    <p>Mot de passe:<input type="password" class="form-control" name="password"></p> 
    <input class="submit btn btn-success pull-right" value="Se connecter" type="submit"> 
</form> 
+0

[Forbidden(403)CSRF驗證失敗。請求中止。即使使用{%csrf \ _token%}](http://stackoverflow.com/questions/20895526/forbidden-403-csrf-verification-failed-request-aborted-even-using-the-csr) –

+0

@AriGold No它不是,一切都設置正確,否則它會告訴我在發展模式和我自己的電腦上的錯誤。 – Lindow

+1

如果所有設置都正確,那麼其他PC似乎有問題...您是否有另一臺PC要測試? – Jingo

回答

0

我解決我的ISS UE通過使用按照本教程中的內置Django的登錄:

我沒有發現,爲什麼我的csrf檢查,因爲有在控制檯上沒有錯誤是不工作的原因並使用{{ csrf_token }}令牌顯示得很好。

相關問題