Various There was, but since I implemented the login function, I will review the procedure.
Reference: http://docs.djangoproject.jp/en/latest/topics/auth.html#web
Since there are convenient modules for both login and logout, it was relatively easy to implement.
views.py
from django.contrib.auth import authenticate #Authentication module
#....
def log_in(req):
from django.contrib.auth import login #For login
user = None #Temporary definition to prevent NameError at GET
if req.method=='POST':
uname = req.POST['username'] #In the login form
pword = req.POST['password'] #Send and receive respectively
user = authenticate( username=uname,password=pword )
#Authenticate user with username and password and assign the result to variable user
if user is not None: #Become None if authentication fails
if user.is_active: #Users who are not active even if they exist cannot log in
login(req,user) #only this. Don't forget the arguments
return HttpResponseRedirect(req.GET['next'])
#At the end of the URL/?next=/page/To make the redirect destination dynamic.
contexts = RequestContext(req,{
'request':req.method,
'user':user,
})
template = loader.get_template('blog_in_heroku/login.html')
return HttpResponse( template.render(contexts) )
def log_out(req):
from django.contrib.auth import logout #For logout
logout(req) #This also seems to require an HttpRequest object as an argument
template = loader.get_template('blog_in_heroku/logout.html')
contexts = Context({})
return HttpResponse( template.render(contexts) )
In addition, following the example, I wrote this time without using any shortcuts. There seems to be a shortcut for authentication, so I think it should be much shorter than this (see the reference page above for details).
login.html
<html>
<head>
<title>Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
<script src="https://code.jquery.com/jquery.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="jumbotron">
<div class="container">
{% ifequal request "POST" %}
{% if not user.is_authenticated %}
<div class="alert alert-danger">
<strong>The input information is incorrect.</strong>
</div>
{% else %}<!--Does not move-->
{% if not user.is_active %}
<div class="alert alert-danger">
<strong>This user has been revoked.</strong>
</div>
{% endif %}
{% endif %}
{% else %}
<div class="alert alert-info">
<h3><strong>Please enter your user name and password.</strong></h3>
</div>
{% endifequal %}
<div class="col-xs-6">
<form class="form-horizontal" role="form" action="" method="post">
{% csrf_token %}
<div class="form-group">
<label for="uname">username</label>
<input type="text" class="form-control input-lg" id="uname" name="username" />
</div>
<div class="form-group">
<label for="pass">password</label>
<input type="password" class="form-control input-lg" id="pass" name="password" />
</div>
<br/>
<div class="form-group">
<input class="btn btn-info btn-lg" type="submit" value="Login" />
<a type="button" href="/" class="btn btn-warning btn-lg">Cancel</a>
</div>
</form>
</div>
<br/>
</div>
</div>
</body>
</html>
It's long because of the script for the error message, but it's good enough just inside the form tag. By the way, the part marked \ <!-Doesn't work-> is ** I tried by deactivating the account that created the account for testing, but it didn't work **, so I commented for the time being.
logout.html
<html>
<head>
<title>Log out</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
<script src="https://code.jquery.com/jquery.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="jumbotron">
<div class="container">
<div class="page-header">
<h2>logged out.</h2>
</div>
<a type="button" class="btn btn-success btn-lg" href="/">back to the top page</a>
<a type="button" class="btn btn-info btn-lg" href="/login/?next=/">Login with another account</a>
</div>
</div>
</body>
</html>
This is a kind of decoration. Pay attention only to the link URL of "Login with another account". ** If you do not specify next properly when writing the view function this time, an error will occur. ** **
What's happening at the moment: http://my-1st-django-blog.herokuapp.com/
Recommended Posts