1、install
- python
- conda create -n env
- source activate env
- conda install django
- conda install mysqlclient
2、first project
django-admin startproject mysite
python manage.py runserver
python manage.py runserver 8080
3、APP
通过manage.py创建app
python manage.py startapp polls
4、ORM
mysite/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
模型
polls/models.py
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)
command
python manage.py syncdb
python manage.py sql polls
python manage.py validate
python manage.py sqlcustom polls
python manage.py sqlclear polls
python manage.py sqlindexes polls
python manage.py sqlall polls
进入交互模式
python manage.py shell
常用方法
p.save()
Poll.objects.all()
Poll.objects.filter()
Poll.objects.get()