先下载django:pip install==2.2.2
django-admin startproject 项目名
运行项目:python manage.py runserver
GET :查看数据
PUT:修改数据
POST:添加数据
DELETE:删除数据
200:成功
400:客户端请求错误
404:客户端资源错误
python manage.py startapp 应用名
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world
")
from myapp import views
# 主路由
urlpatterns = [
path('admin/', admin.site.urls),
path('myapp/',views.index),
]
'DIRS': [os.path.join(BASE_DIR, 'templates')],
hi
真不错
今天这个天气是晴天
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
hi
真不错
今天这个天气是{{ weather }}
{% for var in sequence %}
{{ var }}
{% endfor %}
{% for key,value in sequence.items %}
{{ key }}:{{ value }}
{% endfor %}
{% if test_list %}
列表不为空
{% elif test_dict %}
列表为空,字典不为空
{% else %}
列表字典均为空
{% endif %}
python manage.py makemigrations
python manage.py migrate
# 模型类 会自动生成 id字段
class Game(models.Model):
name = models.CharField(max_length=20, verbose_name='姓名')
atk = models.IntegerField(verbose_name='伤害值')
level = models.IntegerField(verbose_name='等级')
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp', # 注册子应用
]
python manage.py createsuperuser
python -m pip install --upgrade pip
pip install cryptography
from django.contrib import admin
from myapp.models import Game
# Register your models here.
admin.site.register(Game)
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
'DIRS': [os.path.join(BASE_DIR, 'templates')],
from myapp.models import Game
def aaa(request):
game_data = Game.objects.all()
content = {
'games': game_data
}
return render(request, 'index.html', content)
Title
展示数据
编号
姓名
攻击值
等级
{% for game in games %}
{{ game.id }}
{{ game.name }}
{{ game.atk }}
{{ game.level }}
{% endfor %}