Implementing User Registration and Login with Django Views
This tutorial explains how to create Django view functions for user registration and login, covering request handling, rendering templates, database interactions, and URL routing, with complete Python code examples and guidance on using the render function and request methods.
In Django, a view function is a regular Python function that receives an HTTP request and returns an HTTP response; it can return HTML, a redirect, an image, or other content, and is usually defined in the views.py file.
The example below shows how to write login and registration view functions for a simple project.
from django.shortcuts import render
from .models import *
import os
# Create your views here.
def login_views(request):
if request.method == 'GET':
return render(request, 'land/login.html')
elif request.method == 'POST':
uname = request.POST['uname']
upwd = request.POST['upwd']
uList = Users.objects.filter(uname=uname, upwd=upwd)
if uList:
# login failed
if uList[0].isActive is False:
a = '该用户已被禁用!'
return render(request, 'land/login.html', locals())
# login successful
elif uList[0].isActive is True:
nkname = uList[0].nkname
L = Files.objects.filter(uname=uname, isActive=True)
L1 = Files.objects.filter(uname=uname, isActive=False)
return render(request, 'upload/fp.html', locals())
else:
a = '用户或密码错误!'
return render(request, 'land/login.html', locals())
def register_views(request):
if request.method == 'GET':
return render(request, 'land/register.html')
elif request.method == 'POST':
uname = request.POST['uname']
upwd = request.POST['upwd']
nkname = request.POST['nkname']
uemail = request.POST['uemail']
uList = Users.objects.filter(uname=uname)
uList1 = Users.objects.filter(nkname=nkname)
# registration failed
if uList:
a = '注册失败!用户名已存在!'
return render(request, 'land/register.html', locals())
elif uList1:
a = '用户昵称已存在!请重新注册!'
return render(request, 'land/register.html', locals())
else:
# save user info to database
dic = {'uname': uname, 'upwd': upwd, 'nkname': nkname, 'email': uemail}
Users(**dic).save()
# create a folder for the user on the server
LJ = './文件'
folder_name = '%s' % str(uname)
if os.path.isdir(LJ):
os.mkdir(os.path.join(LJ, folder_name))
b = '注册成功!'
return render(request, 'land/login.html', locals())The render function combines a Django template with a context dictionary and returns an HttpResponse object. Its common signature is render(request, template_name[, context]) , where request is the incoming request, template_name is the template file, and context supplies variables for the template.
In a view, request.method determines whether the request is a GET or POST . request.POST returns a QueryDict containing submitted form data, and you can retrieve a specific value with request.POST.get('key') . Similarly, request.GET provides query parameters from a URL.
To expose the view functions via URLs, add entries to urlpatterns in the project's urls.py :
from django.conf.urls import url
from .views import *
urlpatterns = [
url(r'^$', login_views),
url(r'^login$', login_views),
url(r'^register$', register_views),
]With these definitions, the project now supports user registration and login through the defined Django views.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.