This application can recommend movies to different users that are registered on the website. To manage the registration process, we use the standard User
Django module as we have seen in the Models sections. Each page of the website refers to the base.html
page, which implements a top bar that allows the user to register or sign in (right side):
Clicking on one of the two buttons sign in or sign up will activate the code:
<form class="navbar-search pull-right" action="{% url 'auth' %}" method="GET"> {% csrf_token %} <div style="overflow: hidden; padding-right: .5em;"> <input type="submit" name="auth_method" value="sign up" size="30" style="float: right" /> <input type="submit" name="auth_method" value="sign in" size="30" style="float: right" /> </div> </form>
The two methods refer to the urls.py
:
url(r'^auth/', 'books_recsys_app.views.auth', name='auth')
This calls the
auth
function in the views.py
:
def auth(request): if request.method == 'GET': data = request.GET auth_method = data.get('auth_method') if auth_method=='sign in': return render_to_response( 'books_recsys_app/signin.html', RequestContext(request, {})) else: return render_to_response( 'books_recsys_app/createuser.html', RequestContext(request, {})) elif request.method == 'POST': post_data = request.POST name = post_data.get('name', None) pwd = post_data.get('pwd', None) pwd1 = post_data.get('pwd1', None) create = post_data.get('create', None)#hidden input if name and pwd and create: if User.objects.filter(username=name).exists() or pwd!=pwd1: return render_to_response( 'books_recsys_app/userexistsorproblem.html', RequestContext(request)) user = User.objects.create_user(username=name,password=pwd) uprofile = UserProfile() uprofile.user = user uprofile.name = user.username uprofile.save(create=True) user = authenticate(username=name, password=pwd) login(request, user) return render_to_response( 'books_recsys_app/home.html', RequestContext(request)) elif name and pwd: user = authenticate(username=name, password=pwd) if user: login(request, user) return render_to_response( 'books_recsys_app/home.html', RequestContext(request)) else: #notfound return render_to_response( 'books_recsys_app/nopersonfound.html', RequestContext(request))
The function will redirect to the sign up page as shown in the following screenshot:
If you have already registered, it will take you to the sign in page as shown in the following screenshot:
The page allows the user to create a username and password and log in to the website. The data is then used to create a new object of the User Django model and the related UserProfile
object (note that the create
argument is True
to save the object without associating an array of rated movies):
user = User.objects.create_user(username=name,password=pwd) uprofile = UserProfile() uprofile.user = user uprofile.save(create=True) user = authenticate(username=name, password=pwd)
The user is then logged in using the standard Django methods:
from django.contrib.auth import authenticate, login ... login(request, user)
Hence, the website top bar looks like (username:a) as shown in the following screenshot:
Note that in cases where a user with the same name already exists (new sign up exception event) or where a user is not found (sign in exception event), both are implemented and the reader can look into the code to understand how these events are handled.
The sign out button refers to the urls.py
:
url(r'^signout/','books_recsys_app.views.signout',name='signout')
This calls the signout
function from views.py
:
from django.contrib.auth import logout … def signout(request): logout(request) return render_to_response( 'books_recsys_app/home.html', RequestContext(request))
The function uses the standard Django logout method and redirects to the home page (the sign in and sign out buttons will be shown again in the top bar). The user can now search for movies to rate using the information retrieval system (search engine) described in the next section.
3.21.244.217