程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django framework - forms component (Part 2), simple use of modelform, simple use of cookies and sessions

編輯:Python

List of articles

Chapter one django Installation and introduction

Chapter two django Based on using

The third chapter The routing layer

Chapter four A virtual environment 、django Version difference 、 View layer

The fifth chapter Formwork layer

Chapter six The model layer ( On )

Chapter vii. The model layer ( Next )

Chapter viii. ajax

Chapter nine sweetalert Front end plug-ins 、 Serialization component 、 Bulk data operations 、 Pager 、Forms Components ( On )

Chapter ten forms Components ( Next )、ModelForm Easy to use 、cookie And session Easy to use


List of articles

  • List of articles
  • One 、forms Component hook function
    • 1. Local hook
    • 2. global hook
  • Two 、forms Component field parameters
  • 3、 ... and 、forms Component field type
  • Four 、forms Component source code analysis
  • 5、 ... and 、ModelForm Easy to use
  • 6、 ... and 、cookie And session Easy to use
    • cookie brief introduction
    • session
    • django operation cookie
      • Basic use


One 、forms Component hook function

The meaning of hook function is actually to insert additional logic during the execution of the program , The hook function is executed only after the data has been verified by the first layer parameters of the field

Before using hook function, you need to establish form Component class

from django import forms
from app01 import models
from django.core.exceptions import ValidationError
class MyForm(forms.Form):
username = forms.CharField(max_length=20, min_length=6,label=' user name ', error_messages={

'min_length': ' The minimum user name is 6 position ',
'max_lenght': ' The longest user name is 16 position ',
'required': ' The username cannot be empty ',
})
password = forms.CharField(max_length=16, min_length=6,label=' password ',error_messages={

'min_length': ' The minimum user name is 6 position ',
'max_lenght': ' The longest user name is 16 position ',
'required': ' The password cannot be empty ',
})
email = forms.EmailField(required=True,label=' mailbox ', error_messages={

'invalid':' Mailbox format error ',
})

1. Local hook

Local hooks are only for form A field in the component

eg:
Use the local hook to detect whether the user name exists

 def clean_username(self):
username = self.cleaned_data.get('username')
is_has = models.User.objects.filter(username=username)
if is_has:
# self.add_error("username", " User name already exists ") # form Error message method provided by component 
raise ValidationError(' User name already exists ') # Through analysis form Component code discovery can use python To provide error information 
return username

2. global hook

The global hook is for form All fields in the component

eg:
Use the global hook to detect whether the beginning of the password is a capital letter

 def clean(self):
# 1. Get field data 
password = self.cleaned_data.get('password')
if not re.findall('^[A-Z]', password):
self.add_error('password', " The password must start with a capital letter ")
# Finally, the whole data is returned to 
return self.cleaned_data

Two 、forms Component field parameters

Parameter name Parameter function min_length Minimum length max_length Maximum length label Field name error_messages Error message min_value minimum value max_value Maximum initial The default value is validators Regular verifier widget Control the properties of the rendered label choices Select the internal correspondence of the type label

eg:

validators Examples of use

from django.core.validators import RegexValidator
phone = forms.CharField(
validators=[
RegexValidator(r'^[0-9]+$', ' Please enter a number '),
RegexValidator(r'^184[0-9]+$', ' The number must be in 184 start ')],
# RegexValidator You can use multiple relationships between them as and 
)

widget Examples of use

forms.widgets. control type The type of (attrs= Control various properties :class id And so on …)

password = forms.CharField( widget=forms.widgets.PasswordInput(attrs={
'class':'form-control'})
)

3、 ... and 、forms Component field type

Field
required=True, Is it allowed to be empty
widget=None, HTML plug-in unit
label=None, Used to generate Label Label or display content
initial=None, Initial value
help_text='', Help information ( Show... Next to the label )
error_messages=None, error message {
'required': ' Can't be empty ', 'invalid': ' Format error '}
validators=[], Custom validation rules
localize=False, Whether localization is supported or not
disabled=False, Can I edit
label_suffix=None Label Content suffix
CharField(Field)
max_length=None, Maximum length
min_length=None, Minimum length
strip=True Remove user input blanks
IntegerField(Field)
max_value=None, Maximum
min_value=None, minimum value
FloatField(IntegerField)
...
DecimalField(IntegerField)
max_value=None, Maximum
min_value=None, minimum value
max_digits=None, Total length
decimal_places=None, Decimal length
BaseTemporalField(Field)
input_formats=None Time format
DateField(BaseTemporalField) Format :2015-09-01
TimeField(BaseTemporalField) Format :11:12
DateTimeField(BaseTemporalField) Format :2015-09-01 11:12
DurationField(Field) The time interval :%d %H:%M:%S.%f
...
RegexField(CharField)
regex, Custom regular expressions
max_length=None, Maximum length
min_length=None, Minimum length
error_message=None, Ignore , Use of error messages error_messages={
'invalid': '...'}
EmailField(CharField)
...
FileField(Field)
allow_empty_file=False Allow empty files
ImageField(FileField)
...
notes : need PIL modular ,pip3 install Pillow
When the above two dictionaries are used , Two things to note :
- form In the form enctype="multipart/form-data"
- view Function obj = MyForm(request.POST, request.FILES)

Four 、forms Component source code analysis

# 1.
class Form(six.with_metaclass(DeclarativeFieldsMetaclass, BaseForm)):
# 2.
class BaseForm(object):
def __init__(self, data=None, files=None, # Here refers to the enumeration of a part These two parameters are very important 
# 3. some form Common attribute methods of components are BaseForm in 
@property
def errors(self): # How to handle error messages 
def is_valid(self): # form Whether the data obtained by the component is verified as true Methods 
def as_table(self): # form Component rendering tags 
def as_ul(self):# form Component rendering tags 
def as_p(self):# form Component rendering tags 

5、 ... and 、ModelForm Easy to use

forms The components are mainly matched with models The dictation class inside is used together But the fields in the dictation class need to be in forms Class is equivalent to rewriting , Code redundancy, so django take model and form Combined to make a more convenient class , For now, simply use

class MyUser(forms.ModelForm):
class Meta: # The class name here must be Meta
model = models.User # Specifies the associated table 
fields = '__all__' # All fields generate corresponding forms Field 
labels = {
 # label The tag name 
'username': ' user name ',
'password': ' password ',
'email': ' mailbox '
}
widgets = {
 # Label style 
"name": forms.widgets.TextInput(attrs={
"class": "form-control"}),
}

eg:
Code example

1. establish modelform Inheritance class

class MyModelForm(forms.ModelForm):
class Meta:
model = models.User
fields = '__all__'
labels = {

'username': ' user name ',
'password': ' password ',
'email': ' mailbox '
}
widgets = {

"username": forms.widgets.TextInput(attrs={
"class": "form-control"}),
"password": forms.widgets.PasswordInput(attrs={
"class": "form-control"}),
"email": forms.widgets.EmailInput(attrs={
"class": "form-control"}),
}

2. View layer

def register(request):
datas = request.POST
form = MyModelForm()
# user = models.User.objects.filter(username=form.cleaned_data.get('username')).first()
if request.method == 'POST':
# Here you can add a keyword parameter to choose whether to update or add 
form = MyModelForm(data=datas) # Does not add instance So it's new 
# form = MyModelForm(datas, instance=user) # add to instance Specify the image modification of that object, so it is an update 
if form.is_valid():
form.save() # Update or add data 
# models.User.objects.create(**form.cleaned_data)# Use form New data when component 
# models.User.objects.filter(username=form.cleaned_data.get('username')).update(**form.cleaned_data)# Use form Component update data 
return render(request, 'register.html', locals())

3. Formwork layer

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> register </title>
<link rel="stylesheet" href="/static/bootstrap-3.4.1-dist/css/bootstrap.css">
</head>
<body>
<div class="container">
<form action="" method="post">
{% for foo in form %}
<div class="row">
<label>{
{ foo.label }}</label>
{
{ foo }}<span >{
{ foo.errors.0 }}</span>
</div>
{% endfor %}
<input type="submit" value=" register " class="btn-success btn btn-default">
</form>
</div>
</body>
</html>

6、 ... and 、cookie And session Easy to use

cookie brief introduction

Cookie Translated into Chinese means ‘ Cookies ’, By W3C organisation , The earliest by Netscape A mechanism for community development . at present Cookie Has become the norm , All the major browsers such as IE、Netscape、Firefox、Opera Such as support Cookie.

The server does not know the customer's identity from a single network connection . What shall I do? ? Just give the clients a pass , Each one a , Whoever visits must bring his own pass . This allows the server to identify the customer from the pass . This is it. Cookie How it works .

Cookie It is a mechanism for client to save user information , Used to record some information of users , Also realize Session One way .Cookie Limited amount of data stored , And they are all saved in the client browser . Different browsers have different storage sizes , But not more than 4KB. Therefore use Cookie In fact, only a small segment of text information can be stored (key-value Format ).

session

stay WEB In development , The server can create a session object for each user browser (session object ), Be careful : One browser owns one session object ( By default ). therefore , When you need to save user data , The server program can write user data to the exclusive of the user browser session in , When a user uses a browser to access other programs , Other programs can be downloaded from the user's session Take out the user's data , Serving users .

django operation cookie

Basic use

return HttpResonse()
return render()
return redirect()

Do not return the object directly Instead, first use the variable name to refer to Then manipulate the object method
res = HttpResonse()
res.set_cookie()
return res
res = render()
return res
res = redirect()
return res

res.set_cookie() # Set up
res.COOKIE.get() # obtain

There are many view functions that need to add login authentication Sometimes you need to cancel login authentication, and then you need to use decorators

Example :
You cannot enter the homepage or homepage without logging in , Non logged in users will automatically jump to the login page when visiting the home page or home page , But after logging in, you will jump back to the page you want to visit

Login decorator :

from django.shortcuts import redirect
def is_login(func):
def inner(request, *args, **kwargs):
url = request.path
if not request.COOKIES:
return redirect(f'/login/?last={
url}')
res = func(request, *args, **kwargs)
return res
return inner

Login view function :

def login(request):
form = MyForm1()
if request.method == 'POST':
datas = request.POST
form = MyForm1(datas)
if form.is_valid():
username = form.cleaned_data.get('username')
uobj = models.User.objects.filter(username=username)
if uobj:
password = form.cleaned_data.get('password')
if password == uobj.first().password:
if not request.GET:
response = redirect('/home/')
response.set_cookie('username', username)
return response
url = request.GET["last"]
response = redirect(url)
response.set_cookie('username', username)
return response
return render(request, 'login.html', locals())

Home page and view function of home page

@is_login
def home(request):
username = request.COOKIES.get("username")
return render(request, 'home.html', locals())
@is_login
def index(request):
username = request.COOKIES.get('username')
return render(request, 'index.html', locals())

form Components

from django import forms
from app01 import models
from django.core.exceptions import ValidationError
import re
class MyForm(forms.Form):
username = forms.CharField(max_length=20, min_length=6,label=' user name ', error_messages={

'min_length': ' The minimum user name is 6 position ',
'max_lenght': ' The longest user name is 16 position ',
'required': ' The username cannot be empty ',
})
password = forms.CharField(max_length=16, min_length=6,label=' password ',error_messages={

'min_length': ' The minimum user name is 6 position ',
'max_lenght': ' The longest user name is 16 position ',
'required': ' The password cannot be empty ',
})
email = forms.EmailField(required=True,label=' mailbox ', error_messages={

'invalid':' Mailbox format error ',
})
def clean(self):
# 1. Get field data 
password = self.cleaned_data.get('password')
if not re.findall('^[A-Z]', password):
self.add_error('password', " The password must start with a capital letter ")
# Finally, the whole data is returned to 
username = self.cleaned_data.get('username')
is_has = models.User.objects.filter(username=username)
if is_has:
# self.add_error("username", " User name already exists ") # form Error message method provided by component 
raise ValidationError(' User name already exists ') # Through analysis form Component code discovery can use python To provide error information 
return self.cleaned_data
class MyForm1(forms.Form):
username = forms.CharField(max_length=20, min_length=6, label=' user name ', error_messages={

'min_length': ' The minimum user name is 6 position ',
'max_lenght': ' The longest user name is 16 position ',
'required': ' The username cannot be empty ',
})
password = forms.CharField(max_length=16, min_length=6, label=' password ', error_messages={

'min_length': ' The minimum user name is 6 position ',
'max_lenght': ' The longest user name is 16 position ',
'required': ' The password cannot be empty ',
})

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved