在命令行里
1.pip install django
2.cd 想放置项目的文件夹
3.django-admin startproject 项目名称
用pycharm打开创建的项目文件
4. 在项目终端创建app文件夹
django-admin startapp app01
在[settings.py]
'app01.apps.App01Config'
在【urls.py]
path('index/', views.index),
在【views.py]
def index(request):
return HttpResponse("欢迎使用")
在templates编写一个网页 user_list.html
python manage.py runserver
def user_list(request):
return render(request, "user_list.html")
网页会去app里templates中寻找网页文件 (根据app的注册顺序)
视图函数render读取html文件,将占位符等模板语法进行替换,将渲染完成的字符串返回给用户
def tpl(request):
name = 'abc'
roles = ["管理员", "班长", "CEO"]
user_info = {"name":"www", "salary":20000, "age":3}
return render(request, "tpl.html", {"n1": name, "n2": roles, "n3":user_info})
{{ n1 }}
{{ n2.0 }}{{ n2.1 }}{{ n2.2 }}
{% for item in n2%}
{{ item }}
{% endfor %}
{{ n3.name }}
{{ n3.salary }}
{{ n3.age }}
{% for k,v in n3.items %}
{{ k }} = {{ v }} {% endfor %}
也可以单独循环keys和values
{% for k in n3.keys %}
{{ k }} {% endfor %}
{% for v in n3.values %}
{{ v }} {% endfor %}
字典和列表都可以相互嵌套
{% if n1 == "abc" %}
成功
{% elif n1 == "bcd" %}
赢
{% else %}
输
{% endif %}
def news(request):
# https://www.chinaunicom.cn/api/article/NewsByIndex/2/2022/10/news
import requests
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
res = requests.get("https://www.chinaunicom.cn/api/article/NewsByIndex/2/2022/10/news", headers=headers)
data_list = res.json()
print(data_list)
return render(request, "news.html", {"news_list": data_list})
{% for item in news_list %}
- {{ item.news_title }} 时间 {{ item.post_time }}
{% endfor %}
def something(request):
# request 是一个对象, 封装了用户返回的请求相关数据
# 获取请求方式
print(request.method)
# 在url上传值
print(request.GET)
# 在请求体中传值
print(request.POST)
# HttpResponse 内容以字符串返回
#return HttpResponse("成功")
# 返回一个渲染过的网页
#return render(request, "something")
#重定向
return redirect("https://www.baidu.com")
重定向是网页返回浏览器一个地址, 浏览器再去访问这个地址
def login(request): if request.method == "GET": return render(request, "login.html") if request.method == "POST": print(request.POST) username = request.POST.get("user") password = request.POST.get("pwd") if username == "root" and password == "123456": return HttpResponse("登陆成功") else: #return HttpResponse("登陆失败") return render(request, "login.html", {"err_message": "用户名或密码错误"})
要在表单内加一句{% csrf_token %},否则会被forbidden
根据请求方式不同,可以返回不同的结果
不能创建数据库
注意Django4.2只能用mysql8
在[settings.py] 的 INSTALLED_APPS
#把原来的注释掉
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "day1",
"USER": "root",
"PASSWORD": "123456",
"HOST": "127.0.0.1",
"PORT": "3306",
}
}
在[models.py]
class UserInfo(models.Model):
name = models.CharField(max_length=32)
password = models.CharField(max_length=64)
age = models.IntegerField()
在终端
python manage.py makemigrations
python manage.py migrate
当表和字段不需要时候,注释掉在执行终端命令即可。
新增字段,要填写默认值或2为空(避免原来列里有数据)
age = models.IntegerField(default=2)
age = models.IntegerField(null=True, blank=True)
from app01.models import Department, UserInfo
def orm(request):
Department.objects.create(title="IT部")
Department.objects.create(title="运营部")
UserInfo.objects.create(name="www", password="123456", age="22")
return HttpResponse("成功")
def orm(request):
# Department.objects.create(title="IT部")
# Department.objects.create(title="运营部")
# UserInfo.objects.create(name="www", password="123456", age="22")
UserInfo.objects.filter(id=1).delete()
Department.objects.all().delete()
return HttpResponse("成功")
data_list = UserInfo.objects.all()
for obj in data_list:
print(obj.id, obj.name, obj.password, obj.age)
UserInfo.objects.all().update(password=999)
UserInfo.objects.filter(id=2).update(name="afre")
UserInfo.objects.filter(name="aaa").update(age=77)