django快速建站

发布时间 2023-09-25 09:17:57作者: 凤毛麟角编程
# pip install django
# pip install requests

import os
import time

def createfile(filepath,filetext):
    if not os.path.exists(filepath):
        with open(filepath,'w',encoding='utf-8') as file:
            file.write(filetext)
if not os.path.exists('./myproject'):
    createfile('djangocreate.bat',
    '''django-admin startproject myproject
cd myproject
django-admin startapp myapp
python manage.py migrate''')
    os.system('start djangocreate.bat')

while True:
    time.sleep(1)
    if os.path.exists('./myproject'):
        if not os.path.exists('./myproject/static'):
            os.mkdir('./myproject/static')
            os.mkdir('./myproject/static/img')
            os.mkdir('./myproject/static/css')
            os.mkdir('./myproject/static/js')
        if not os.path.exists('./myproject/templates'):
            os.mkdir('./myproject/templates')
        break
def addfile(filepath,addtext):
    while True:
        time.sleep(1)
        if os.path.exists(filepath):
            with open(filepath,'r',encoding='utf-8') as file:
                filetext = file.read()[-len(addtext):]
            if addtext != filetext:
                with open(filepath,'a+',encoding='utf-8') as file:
                    file.write(addtext)
            break
addfile(
    './myproject/myproject/settings.py',
    '''
import os
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]
TEMPLATES[0]['DIRS'] = [os.path.join(BASE_DIR, 'templates')]
INSTALLED_APPS = INSTALLED_APPS + ['myapp']
ALLOWED_HOSTS = ALLOWED_HOSTS + ['*']''')
addfile(
    './myproject/myapp/models.py',
    '''
class BookInfo(models.Model):
    name = models.CharField(max_length=10)
    def __str__(self):
        return self.name
class PeopleInfo(models.Model):
    name = models.CharField(max_length=10)
    gender = models.BooleanField()
    book = models.ForeignKey(BookInfo,on_delete=models.CASCADE)''')
addfile(
    './myproject/myapp/admin.py',
    '''
from myapp.models import BookInfo,PeopleInfo
admin.site.register(BookInfo)
admin.site.register(PeopleInfo)''')


while True:
    time.sleep(1)
    if os.path.exists('./myproject/manage.py'):
        createfile('djangorun.bat',
    '''cd myproject
python manage.py makemigrations
python manage.py runserver 127.0.0.1:8000''')
        os.system('start djangorun.bat')
        break

createfile('djangoreg.bat',
    '''cd myproject
python manage.py createsuperuser''')
while True:
    time.sleep(1)
    if os.path.exists('./myproject/db.sqlite3'):
        os.system('start djangoreg.bat')
        break

# http://127.0.0.1:8000
# http://127.0.0.1:8000/admin/