In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to log in and register with Python Django". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1.modles layer and admin
Click (here) to collapse or open
#-*-coding: utf-8-*-
From _ _ future__ import unicode_literals
From django.db import models
From django.contrib.auth.models import AbstractUser
From django.utils.encoding import python_2_unicode_compatible
# data model of ordinary users
@ python_2_unicode_compatible
Class NewUser (AbstractUser):
Profile = models.CharField ('profile', default='', max_length=256)
Def _ str__ (self):
Return self.username
# Create your models here.
# # admin
#-*-coding: utf-8-*-
From django.contrib import admin from .models import NewUser class NewUserAdmin (admin.ModelAdmin): list_display = ('username','date_joined',' profile') admin.site.register (NewUser,NewUserAdmin)
2.setting sets the account of newuser management system for verification
AUTH_USER_MODEL = "pric.NewUser"
3 views view layer
#-*-coding: utf-8-*-
From django.shortcuts import render
From .forms import *
From .models import *
From django.contrib.auth import authenticate, login, logout
From django.http import HttpResponse
From django.shortcuts import render, redirect,render_to_response
From django.core.exceptions import ObjectDoesNotExist
From django.contrib.auth.decorators import login_required
@ login_required
Def index (request):
Return render_to_response ('index.html', locals ())
# Login
Def log_in (request):
If request.method = 'GET':
Form = LoginForm ()
Return render (request, 'login.html', {' form': form})
If request.method = = 'POST': # when submitting the form
Form = LoginForm (request.POST) # form contains submitted data
If form.is_valid (): # if the submitted data is legal
Username = form.cleaned_data ['uid']
Password = form.cleaned_data ['pwd']
User = authenticate (username=username, password=password)
If user is not None: # if user authentication exists
Login (request, user) # start logging in
Return redirect ('index') # Jump home page
Else:
Return render (request, 'login.html', {' form': form, 'msg': "password or username is not ture!"})
Else: # if the submitted data is illegal, continue to login
Return render (request, 'login.html', {' form': form})
# Logout
@ login_required
Def log_out (request):
Logout (request)
Return redirect ('login')
# Create your views here.
# Registration
Def register (request):
Error1 = "this name is already exist"
Valid = "this name is valid"
If request.method = 'GET':
Form = RegisterForm ()
Return render (request, 'register.html', {' form': form})
If request.method = = 'POST': # submit
Form = RegisterForm (request.POST)
If form.is_valid ():
Username = form.cleaned_data ['username']
Email = form.cleaned_data ['email']
Password1 = form.cleaned_data ['password1']
Password2 = form.cleaned_data ['password2']
If password1! = password2:
Return render (request, 'register.html', {' form': form, 'msg': "two password is not equal"})
Else:
NewUser.objects.create_user (username=username,password=password1,email=email)
# user = NewUser (username=username, email=email, password=password1)
# user.save ()
# return render (request, 'login.html', {' success': "you have successfully registered!"})
Return redirect ('login')
Else:
Return render (request, 'register.html', {' form': form})
4 Home page to get the currently logged-in user
Method 1:
The front-end request.user.is_authenticated () determines whether the current user is logged in
Welcome! >
{% if request.user.is_authenticated%}
Hihi: {{request.user}}
Log out
{% endif%}
Method 2 writes the request.user.is_authenticated judgment into the views view and then passes it into the html
@ login_required
Def index (request):
If request.user.is_authenticated (): # determine whether the user is logged in
User = request.user
Return render_to_response ('index.html', locals ())
Welcome! >
Welcome to login! {{user}}
Log out
This is the end of the content of "how to log in and register with Python Django". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.