个人Django项目的创建流程,个人开发习惯

1、django-admin startproject djangoDemo

2、python manage.py startapp app01

3、注册app:app01.apps.App01Config

4、数据库连接配置(需要安装mysqlclient):

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydatabase',
        'USER': 'root',
        'PASSWORD': 'abin0033',
        'HOST': '127.0.0.1',
        'PORT': 3306,
    }
}

5、在app01下的models.py里创建表结构

class UserInfo(models.Model):
    # id = models.AutoField() 不用创建,自动生成并且是主键
    name = models.CharField(verbose_name="姓名", max_length=16)
    age = models.IntegerField(verbose_name="年龄")
    email = models.CharField(verbose_name="邮箱", max_length=32)

6、执行生成表结构的命令

python manage.py makemigrations

python manage.py migrate

7、在urls.py中导入app01.views 并添加添加视图

urlpatterns = [
    path('admin/', admin.site.urls),
    path('user_add/', views.user_add),
]

8、在app01下views.py 中添加视图函数

def user_add(request):
    return render(request, "user_add.html")
    #return HttpResponse("欢迎使用")

9、表中数据的ORM操作

def user_add(request):
    # 新增
    models.UserInfo.objects.create(name='亚瑟王', age=19, email='[email protected]')
    models.UserInfo.objects.create(name='亚索', age=19, email='[email protected]')
    models.UserInfo.objects.create(**{'name': '提莫', 'age': 19, 'email': '[email protected]'})
    # 查询
    v1 = models.UserInfo.objects.filter(name='亚索', age=19)
    v2 = models.UserInfo.objects.all()
    v3 = models.UserInfo.objects.filter(name='亚索', age=19).first()
    v4 = models.UserInfo.objects.all().order_by('id') #asc  -id是desc
    # 删除
    models.UserInfo.objects.filter(id=1).delete()
    # 更新
    models.UserInfo.objects.filter(id=2).update(age=20)
    return render(request, "user_add.html")

你可能感兴趣的:(Django,django,python,后端)