我想創建一個頭部,並要求使用用戶名/密碼和登錄按鈕進行登錄。目前,我如何設置我的頁面是按下登錄會將我發送到登錄頁面。我只想輸入信息並按「登錄」登錄我的網站的主頁。我如何設計我的urls.py
和views.py
以在主頁上執行登錄?首頁登錄表單Django
我有一個base.html
這是我的主頁的模板。在模板中,我做了一個login.html
局部視圖:
<form action='/accounts/auth/' method='POST'> {% csrf_token %}
<div >
<label for='username'> Username </label>
<input type='text' name='Username' id='username'>
<label for='password'>Password </label>
<input type='password' name='Password' id='password'>
<input type='submit' value='login'>
</div>
</form>
我有點糊塗了action
屬性因爲我不知道在哪裏發送表單數據,如果我想授權在同一頁面上登錄。
我views.py
def login(request):
c = {}
c.update(csrf(request))
return render(request, 'login.html', c)
def auth_view(request):
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = auth.authenticate(username = username, password = password)
if user is not None:
auth.login(request, user)
return HttpResponseRedirect('/accounts/loggedin')
else:
return HttpResponseRedirect('/accounts/invalid')
我不知道在哪裏HttpResponseRedirect
,以及如果記錄中所有的主頁上完成的。
也許我可以做一個render(request,SomePartialView.html)
而不是HttpResponseRedirect
。
這裏是我的urls.py:
url(r'^$', 'photoblog.views.login'), #displays login.html
url(r'^accounts/auth/$', 'photoblog.views.auth_view'), #authorize login
我試過之後你的意見,但我不斷收到 「HomeView」 沒有定義。我嘗試導入到網址,但沒有奏效。我錯過了什麼? –
@MichelleGlauser - 導入應該是所有必要的。我可以從遠處推薦的是檢查錯別字和不正確的路徑。 –
如何更改登錄的默認模板位置?我沒有看到在哪裏放template_name – shenk