查看安装的Django版本html
py -m django --version
复制代码
若你用 pip 安装 Django,你可使用 --upgrade
或 -U
标志进行更新python
$ pip install -U Django
复制代码
此文章是3.0版本mysql
进入正文sql
建立项目 |
打开命令行,cd 到一个你想放置你代码的目录,而后运行如下命令:shell
django-admin startproject mysite
复制代码
mysite即项目名称数据库
看一下 startproject 建立的内容django
mysite/
manage.py //一个让你用各类方式管理 Django 项目的命令行工具
mysite/
__init__.py
settings.py //Django 项目的配置文件
urls.py //Django 项目的 URL 声明
asgi.py //做为你的项目的运行在 ASGI 兼容的Web服务器上的入口
wsgi.py //做为你的项目的运行在 WSGI 兼容的Web服务器上的入口
复制代码
运行项目,命令行输入如下命令浏览器
py manage.py runserver
复制代码
成功的话,会看到如下输出bash
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
四月 01, 2020 - 15:50:53
Django version 3.0, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
复制代码
浏览器访问 https://127.0.0.1:8000/ 你将会看到一个“祝贺”页面,随着一只火箭发射,服务器已经运行了。服务器
你也能够更换端口
py manage.py runserver 8080
复制代码
案例:建立投票应用 |
项目和应用有什么区别?
应用是一个专门作某件事的网络应用程序——好比博客系统,或者公共记录的数据库,或者小型的投票程序。
项目则是一个网站使用的配置和应用的集合。项目能够包含不少个应用。应用能够被不少个项目使用
处于 manage.py 所在的目录下,而后运行这行命令来建立一个应用:
py manage.py startapp polls
复制代码
这将会建立一个 polls 目录,它的目录结构大体以下:
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
复制代码
编写第一个视图 |
在 polls/urls.py 中,输入以下代码:
polls/urls.py |
---|
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ] |
下一步是要在根 URLconf 文件中指定咱们建立的 polls.urls 模块。在 mysite/urls.py 文件的 urlpatterns 列表里插入一个 include(), 以下:
mysite/urls.py |
---|
from django.contrib import admin from django.urls import include, path urlpatterns = [path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ] |
输入命令:
python manage.py runserver
复制代码
用浏览器访问 http://localhost:8000/polls/, 你应该可以看见 "Hello, world. You're at the polls index." ,这是你在 index 视图中定义的。
数据库配置 |
默认是SQLite
原本想配mysql,待简单学习后再配置
编辑 mysite/settings.py 文件前,先设置 TIME_ZONE 为你本身时区,顺便修改一下语言。
Django有自带的默认应用
默认开启的某些应用须要至少一个数据表,因此,在使用他们以前须要在数据库中建立一些表。请执行如下命令:
py manage.py migrate
复制代码
建立模型 |
好比在这个投票应用中,须要建立两个模型:问题 Question 和选项 Choice。
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) |
激活模型 |
首先得把 polls 应用安装到咱们的项目里
在配置类 INSTALLED_APPS 中添加设置。由于 PollsConfig 类写在文件 polls/apps.py 中,因此它的点式路径是 'polls.apps.PollsConfig'。在文件 mysite/settings.py 中 INSTALLED_APPS 子项添加点式路径后,它看起来像这样:
mysite/settings.py |
---|
INSTALLED_APPS = [ 'polls.apps.PollsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] |
py manage.py makemigrations polls
py manage.py migrate
复制代码
因此改变模型须要这三步:
尝试API |
输入如下命令进入交互python命令行
py manage.py shell
复制代码
你能够输入如下来进行测试database
>>> from polls.models import Choice, Question # Import the model classes we just wrote.
# No questions are in the system yet.
>>> Question.objects.all()
<QuerySet []>
# Create a new Question.
# Support for time zones is enabled in the default settings file, so
# Django expects a datetime with tzinfo for pub_date. Use timezone.now()
# instead of datetime.datetime.now() and it will do the right thing.
>>> from django.utils import timezone
>>> q = Question(question_text="What's new?", pub_date=timezone.now())
# Save the object into the database. You have to call save() explicitly.
>>> q.save()
# Now it has an ID.
>>> q.id
1
复制代码
Django管理页面 |
建立一个管理员帐户:
py manage.py createsuperuser
复制代码
启动:
py manage.py runserver
复制代码
进入 http://127.0.0.1:8000/admin/
你会看到管理员页面
在后台能够进行数据编辑
注意建立的应用要进行接口才能查看
打开 polls/admin.py 文件,把它编辑成下面这样:
polls/admin.py |
---|
from django.contrib import admin from .models import Question admin.site.register(Question) |
再次打开管理页面便可对应用的数据进行编辑