pip install django
pip install --default-timeout=6000 -i https://pypi.tuna.tsinghua.edu.cn/simple django
django-admin --version
這條命令驗證。django-admin startproject mysite
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
cd mysite
django-admin startapp polls
mysite/
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
mysite/
manage.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls.apps.PollsConfig', # 添加
]
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '主機',
'PORT': 端口,
'NAME': '數(shù)據(jù)庫名',
'USER': '用戶名',
'PASSWORD': '密碼'
}
}
pip install pymysql
__init__
文件中添加:import pymysql
pymysql.version_info = (1, 4, 0, "final", 0)
pymysql.install_as_MySQLdb()
python manage.py makemigrations polls
Migrations for 'polls':
polls/migrations/0001_initial.py
- Create model Question
- Create model Choice
python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
Rendering model states... DONE
Applying polls.0001_initial... OK
python manage.py runserver
聯(lián)系客服