1
1
from django .contrib .auth import authenticate , login , logout
2
- from django .contrib .auth .forms import UserCreationForm
2
+ from django .contrib .auth .forms import AuthenticationForm , UserCreationForm
3
3
from django .shortcuts import render , redirect
4
4
5
5
def register_view (request ):
@@ -12,19 +12,19 @@ def register_view(request):
12
12
13
13
# Create your views here.
14
14
def login_view (request ):
15
+ # future -> ?next=/articles/create/
15
16
if request .method == "POST" :
16
- username = request .POST .get ("username" )
17
- password = request .POST .get ("password" )
18
- # remove this!!!!
19
- # print(username, password)
20
- # remove this!!
21
- user = authenticate (request , username = username , password = password )
22
- if user is None :
23
- context = {"error" : "Invalid username or password." }
24
- return render (request , "accounts/login.html" , context )
25
- login (request , user )
26
- return redirect ('/' )
27
- return render (request , "accounts/login.html" , {})
17
+ form = AuthenticationForm (request , data = request .POST )
18
+ if form .is_valid ():
19
+ user = form .get_user ()
20
+ login (request , user )
21
+ return redirect ('/' )
22
+ else :
23
+ form = AuthenticationForm (request )
24
+ context = {
25
+ "form" : form
26
+ }
27
+ return render (request , "accounts/login.html" , context )
28
28
29
29
30
30
def logout_view (request ):
0 commit comments