pip install django==2.2.* -i https://mirrors.aliyun.com/pypi/simple/
python -m django --version
django-admin startproject myweb
python manage.py runserver
http://X.X.X.X:8080/
DisallowedHost at /
Invalid HTTP_HOST header: '192.168.0.108'. You may need to add '192.168.0.108' to ALLOWED_HOSTS.
HTTP——HOST标头无效:'192.168.*.*:8000'。你需要将'192.168.*.*:8000'添加到ALLOWED_HOSTS
--修改配置文件-settings.py
1、
ALLOWED_HOSTS = ["192.168.0.108"]
2、
ALLOWED_HOSTS = ["192.168.0.108","localhost","127.0.0.1"]
3、
ALLOWED_HOSTS = ["*"] # 支持所有
python manage.py startapp myapp
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello World!")
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
"""myweb URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path("myapp/", include('myapp.urls'))
]
pip install mysqlclient -i https://mirrors.aliyun.com/pypi/simple/
https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient
pip install mysqlclient-1.4.6-cp37-cp37m-win32.whl
from django.db import models
class Stu(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=16)
age = models.SmallIntegerField()
sex = models.CharField(max_length=1)
classid = models.CharField(max_length=8)
def __str__(self):
return "%d:%s:%d:%s:%s"%(self.id,self.name,self.age,self.sex,self.classid)
class Meta:
db_table="stu"
--创建mydemo数据库
create database mydemo
--使用mydemo数据库
use mydemo
--查看当前数据库
select database();
--建表 stu
CREATE TABLE if not EXISTS `stu` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` varchar(16) NOT NULL,
`age` tinyint(3) NOT NULL,
`sex` char(1) NOT NULL,
`classid` char(8),
PRIMARY KEY (`id`)
);
--查看所有表
show tables
--查看stu表数据
select *from stu
--删除stu表
drop table stu
--删除stu表所有数据
DELETE FROM stu
--插入数据
INSERT into stu
(id,name,age,sex,classid)
VALUES
('1','小明','14','男','001'),
('2','小红','13','女','001'),
('3','小强','11','男','002'),
('4','小伟','15','男','002'),
('5','小丽','14','女','003'),
('6','小美','13','女','003'),
('7','小红','12','女','004'),
('8','小军','16','男','004')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydemo',
'USER': 'root',
'PASSWORD': '12345687',
'HOST': 'localhost',
'PORT': '3306',
}
}
myapp-->views.py
from django.shortcuts import render
from django.http import HttpResponse
from myapp.models import Stu
# Create your views here.
def index(request):
return HttpResponse("Hello World!")
def add(request):
lists = Stu.objects.all() # 获取数据库stu表的所有数据
for stu in lists:
print(stu)
print(Stu.objects.get(id=6)) # 获取其中一条数据
return HttpResponse("bye")
python manage.py migrate
python manage.py createsuperuser
ps:邮箱可以不填,输入密码是不显示的(建议8位)
myweb-->settings.py
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True
myapp-->admin.py
from django.contrib import admin
# Register your models here.
from myapp.models import Stu
admin.site.register(Stu)
myapp-->models.py
from django.db import models
class Stu(models.Model):
'''自定义Stu表对应的Model类'''
# 定义属性:默认主键自增id字段可不写
id = models.AutoField("学号",primary_key=True)
name = models.CharField("姓名",max_length=16)
age = models.SmallIntegerField("年龄")
sex = models.CharField("性别",max_length=1)
classid = models.CharField("班级",max_length=8)
def __str__(self):
return "%d:%s:%d:%s:%s"%(self.id,self.name,self.age,self.sex,self.classid)
class Meta:
db_table="stu"
verbose_name = '浏览学生信息'
verbose_name_plural = '学生信息管理'
myapp-->admin
from django.contrib import admin
# Register your models here.
from myapp.models import Stu
# stu模型的管理器(装饰器写法)
@admin.register(Stu)
class StuAdmin(admin.ModelAdmin):
# listdisplay 设置要显示在列表中的字段(id字段是Django模型的默认主键)
list_display = ('id','name','age','sex','classid')
# 设置哪些字段可以点击进入编辑界面
list_display_links = ('id','name')
# list_per_page设置每页显示多少条记录,默认是100条
list_per_page = 10
#orderring设置默认排序字段,负号表示降序排序
ordering = ('id',) # -id降序
#list_editable 设置默认可编辑字段
#list_editable = ['age','sex','classid']