Django2

database

'ENGINE': 'django.db.backends.sqlite3'
'ENGINE': 'django.db.backends.postgresql'
'ENGINE': 'django.db.backends.mysql'
'ENGINE': 'django.db.backends.oracle'
'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
'NAME': 'mydatabase', 
'USER': 'mydatabaseuser', 
'PASSWORD': 'mypassword', 
'HOST': '127.0.0.1', 
'PORT': '5432',

数据库表生成:

python manage.py migrate

pools.models.py

from django.db import models

# Create your models here.
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)

django1.setting.py

INSTALLED_APPS = [
    'pools.apps.PoolsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
python manage.py makemigrations pools
python manage.py sqlmigrate pools 0001
python manage.py migrate

进入命令行模式:

python manage.py shell
from pools.models import Question, Choice
Question.objects.all()
from django.utils import timezone
q = Question(question_text="What's new?", pub_date=timezone.now())
q.save()
Question.objects.all()
q.id
q.question_text
q.pub_date
q.question_text = "What's up?"
q.save()
q.question_text

adminuser

python manage.py createsuperuser
//test 123456abc

pools/admin.py

from django.contrib import admin
from .models import Question

admin.site.register(Question)

http://localhost:8000/admin

mysql数据库提示:no module named 'MySQLdb'
分析:MySQLdb并不支持python3.4
解决方案:替代品pymysql,pymysql的用法与MySQLdb完全相同。

Django2_第1张图片
Paste_Image.png

init.py设置:

import pymysql
pymysql.install_as_MySQLdb()

参考

centos下安装python3.x+django的mysql驱动
PyMySQL

你可能感兴趣的:(Django2)