Library management system
Press book authors
Press management
Show
# 查询所有的数据
all_publishers = models.Publisher.objects.all() # 对象列表
render(request,'publisher_list.html',{'all_publishers':all_publishers})
Template syntax:
{{All_publishers}} variable
{% for i in all_publishers %}
{{ forloop.counter }} {{ i }}
{% endfor %}
New
# 方式一
ret = models.Publisher.objects.create(name=pub_name)
# 方式二
obj = models.Publisher(name=pub_name)
obj.save()
delete
pk = request.GET.get('pk')
query = models.Publisher.objects.filter(pk=pk) # 对象列表
query.delete() # 通过queryset 删除
query[0].delete() # 通过单独的对象 删除
edit
obj = models.Publisher.objects.filter(pk=pk).first() # 对象列表中第一个对象
obj.name = pub_name # 内存中修改
obj.save() # 提交
django command
download
pip install django==1.11.23 - i
Create a project
django-admin startproject project name
Startup project
Cd to the root directory of the project
python manage.py runserver # 127.0.0.1:8000
python manage.py runserver 80 # 127.0.0.1:80
python manage.py runserver 0.0.0.0:80 # 0.0.0.0:80
Creating app
python manage.py startapp app name
registered
Database Migration
python manage.py makemigrations # create models under APP registered migration file detection
python manage.py migrate # migration will change records to the database synchronization models
django configuration settings
INSTALLED_APPS = [
"App01,
'app01.apps.App01Config'
]
DATABASES
ENGINE: Engine
NAME: The name of the database
HOST : IP
PORT : 3306
USER: User Name
PASSWORD: Password
Static files
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,‘static’)
]
Middleware
Comment out csrf submit POST
Templates TEMPLATES
DIRS =[ os.path.join(BASE_DIR,‘templates’)]
mysql database using django process
Create a mysql database
settings in the configuration database
ENGINE: Engine
NAME: The name of the database
HOST : IP
PORT : 3306
USER: User Name
PASSWORD: Password
Tell django modules are connected using pymysql mysql database;
In the same directory with the settings of
__init__.py
writeimport pymysql
pymysql.install_as_MySQLdb()
Write in class under the app in models.py
class Publisher(models.Model): name = models.Charfield(max_length=32) # varcher(32)
The migration command:
python manage.py makemigrations # create models under APP registered migration file detection
python manage.py migrate # migration will change records to the database synchronization models
get and post the difference
get
Send get requests ways:
- form forms do not specify the method
- Enter directly enter the address in the address bar
- a title
?k1=v1&k2=v2 request.GET request.GET.get('k1')
post
form form method = 'post'
request.POST request.POST.get('k1')
snake
Directed to an object and a mapping of relational database
Map:
Class - "Table
Object - "records (rows)
Properties - "field
from app import models
Inquire:
models.Publisher.objects.get (name = 'xxxxx') # object has a query and a plurality of unique or almost on the error
models.Publisher.objects.filter (name = 'xxxxx') # query all objects in the object list to meet the conditions queryset
models.Publisher.objects.all () # query all data objects list queryset
Add:
models.Publisher.objects.create (name = 'xxxxx') # return value is the new object
obj = models.Publisher(name='xxxxx') obj.save()
delete:
models.Publisher.objects.filter(pk=1) .delete()
obj = models.Publisher.objects.filter(pk=1) .first() obj.delete()
modify:
obj = models.Publisher.objects.filter(pk=1) .first()
obj.name = 'xxxxx'
obj.save()
The syntax of the template
render (request, 'the template file name', { 'k1': v1})
variable
{K1} {}
for loop
{% for i in k1 %}
{{ forloop.counter }}
{{ i }}
{% endfor %}
Foreign key
Many
Library management system
Press
book
Foreign key design
class Book(models.Model): title = models.CharField(max_length=32) pid = models.ForeignKey('Publisher', on_delete=models.CASCADE) # 外键 # on_delete 2.0 必填
Inquire:
all_books = models.Book.objects.all()
print(all_books)
for book in all_books:
print(book)
print(book.pk)
print(book.title)
print(book.pub,type(book.pub)) # 所关联的对象
print(book.pub_id,type(book.pub_id)) # 所关联的对象的pk
print('*' * 32)
New
models.Book.objects.create(title=title,pub=models.Publisher.objects.get(pk=pub_id))
models.Book.objects.create(title=title, pub_id=pub_id)
delete:
pk = request.GET.get('pk')
models.Book.objects.filter(pk=pk).delete()
edit:
book_obj.title = title
book_obj.pub_id = pub_id
# book_obj.pub = models.Publisher.objects.get(pk=pub_id)
book_obj.save()