环境准备
- 安装Django:
sudo apt-get install python3-dev
sudo apt-get install python3-pip
sudo pip3 install Django=1.11.4
- 验证安装
python3 -m django --version
1.11.4
默认项目
django-admin startproject mysite
#django-admin.py startproject mysite 如果上面的命令提示不存在执行这条命令
- 项目目录结构:
mysite/ #项目空间
manage.py #命令行模式
mysite/ #项目名称
__init__.py
settings.py #配置项
urls.py #url路由
wsgi.py
- 执行
cd mysite
python3 manage.py runserver 8080
在浏览器中打开http://127.0.0.1:8080
可以看到欢迎信息。
自定义项目
在mysite目录下:
python manage.py startapp polls
ls
db.sqlite3 manage.py mysite polls
上述命令类似于在项目下新建一个子module,以便完成不同的任务。
下面开始新建自己页面以及页面逻辑。
编写的顺序为
- 新建页面
- 新建url路由
- 在根url路由中注册
新建页面
gedit polls/views.py
views.py中添加如下代码:
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse('hello world from pools')
添加url路由
gedit polls/urls.py
添加如下代码:
from django.conf.urls import url
from polls import views
urlpatterns=[url(r'^$',views.index,name='index')]
注册路由
gedit mysite/urls.py
urls.py中添加如下代码:
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/',include('polls.urls')),#add
]
好了,以上就完成了自定义的界面和url。
mysite目录下终端执行:
python3 manage.py runserver
打开http://127.0.0.1:8080/polls
可以看到hello world from pools
字样
添加数据库支持
打开polls/models.py
# model定义了项目的数据结构
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)
打开mysite/settings.py修改:
# Application definition
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
终端执行:
python3 manage.py makemigrations
python3 manage.py migrate
准备数据
python3 manage.py shell
In [1]: from polls.models import Question, Choice
In [2]: Question.objects.all()
Out[2]: <QuerySet []>
In [3]: from django.utils import timezone
In [4]: q=Question(question_text='what\'s new?',pub_data=timezone.now())
In [5]: q.save()
In [6]: q.id
Out[6]: 1
In [7]: q.question_text
Out[7]: "what's new?"
In [8]: q.pub_date
Out[8]: datetime.datetime(2017, 8, 19, 10, 13, 27, 102521, tzinfo=<UTC>)
In [9]: Question.objects.all()
Out[9]: <QuerySet [<Question: Question object>]>
# Create three choices.
In [10]: q.choice_set.create(choice_text='Not much', votes=0)
Out[10]:<Choice: Not much>
In [11]: q.choice_set.create(choice_text='The sky', votes=0)
Out[11]:<Choice: The sky>
In [12]: q.choice_set.create(choice_text='Just hacking again', votes=0)
Out[12]:<Choice: Just hacking again>
admin
python3 manage.py createsuperuser #创建admin账户
python3 manage.py runserver
启动网站,在浏览器输入http://127.0.0.1:8080/admin
可以进入admin登陆界面,输入admin账户帐号和密码即可以进行登录。
可以看到并没有Question的管理选项。
打开polls/admin.py
添加:
from django.contrib import admin
from .models import Question
admin.site.register(Question)
刷新之后可以看到:
即可以对Question进行管理。
以上就完成了基本的Django的学习。