当项目中的models有数据表的时候,普通做法是用docke exec -it hello_web_1 bash,进入容器进行migrate,但是我们想要容器一启动就自动创建数据表,可以修改docker-compose.yml中的command命令,如下:
version: '3'
services:
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: 123456
web:
build: .
command:
- /bin/bash
- -c
- |
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py runserver 0.0.0.0:8000
ports:
- 8000:8000
depends_on:
- db
当我们需要登陆的时候,需要数据库中有user用户,使用django自带认证方法:
from django.shortcuts import render, redirect
from django.views import View
from django.contrib.auth import authenticate, login, logout, models
class thick_admin(View):
"""检测是否是第一次使用"""
def get(self, request):
exist_tag = "true"
exist = models.User.objects.count()
print('exist', exist)
if exist == 0:
exist_tag = "false"
return render(request, "register.html", locals())
def post(self, request):
username = request.POST.get('username')
password = request.POST.get('password')
is_superuser = request.POST.get('is_superuser')
if is_superuser:
models.User.objects.create_superuser(username=username, password=password, email=None)
print('superuser')
else:
models.User.objects.create_user(username=username, password=password)
print('user')
return redirect('/login/')
手机扫一扫
移动阅读更方便
你可能感兴趣的文章