Python 3.7.7 (v3.7.7:d7c567b08f, Mar 10 2020, 02:56:16)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
等待一会后,用命令输入
>>> import django
>>> print(django.get_version())
3.0.5
python -m django --version
3.0.5
目录结构为:manage.py
/
根目录只是你项目的容器, 根目录名称对Django没有影响,你可以将它重命名为任何你喜欢的名称。manage.py
: 一个让你用各种方式管理 Django 项目的命令行工具。/
目录包含你的项目,它是一个纯 Python 包。它的名字就是当你引用它内部任何东西时需要用到的 Python 包名。 (比如 django3demo.urls).mysite/__init__.py
:一个空文件,告诉 Python 这个目录应该被认为是一个 Python 包。mysite/settings.py
:Django 项目的配置文件。mysite/urls.py
:Django 项目的 URL 声明,就像你网站的“目录”。mysite/asgi.py
:作为你的项目的运行在 ASGI 兼容的Web服务器上的入口。mysite/wsgi.py
:作为你的项目的运行在 WSGI 兼容的Web服务器上的入口。
让我们来确认一下你的 Django 项目是否真的创建成功了。如果你的当前目录不是外层的 django3demo 目录的话,请切换到此目录,然后运行下面的命令:注意启动的目录是在django3demo目录下
python manage.py runserver
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
April 08, 2020 - 03:51:21
Django version 3.0.5, using settings 'django3demo.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
关闭用ctrl+c关闭服务
python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying sessions.0001_initial... OK
重新启动后没有红色提示
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
April 08, 2020 - 03:57:21
Django version 3.0.5, using settings 'django3demo.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
$python manage.py startapp polls
生成目录如下图
polls/views.py
polls.urls
模块。在 点击ango3demo/urls.py
文件的 urlpatterns
列表里插入一个 include()
, 如下:from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
备注:当包括其它 URL 模式时你应该总是使用 include()
, admin.site.urls
是唯一例外。
python manage.py createsuperuser
输入用户名和邮箱和密码
127.0.0.1:8000/admin
polls/admin.py
import datetime
from django.test import TestCase
from django.utils import timezone
from .models import Question
class QuestionModelTests(TestCase):
def test_was_published_recently_with_future_question(self):
"""
was_published_recently() returns False for questions whose pub_date
is in the future.
"""
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
self.assertIs(future_question.was_published_recently(), False)
python manage.py test polls
运行出现错误,没有通过,
Traceback (most recent call last):
File "/Users/apple/PythonProjects/django3demo/polls/tests.py", line 18, in test_was_published_recently_with_future_question
self.assertIs(future_question.was_published_recently(), False)
AttributeError: 'Question' object has no attribute 'was_published_recently'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
失败记录
test_was_published_recently_with_future_question
方法中,它创建了一个 pub_date
值为 30 天后的 Question
实例。assertls()
方法,发现 was_published_recently()
返回了 True
,而我们期望它返回 False
。python manage.py test polls
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..
----------------------------------------------------------------------
Ran 2 tests in 0.002s
OK
建立测试,增加2个test
def test_was_published_recently_with_old_question(self):
"""
was_published_recently() returns False for questions whose pub_date
is older than 1 day.
"""
time = timezone.now() - datetime.timedelta(days=1, seconds=1)
old_question = Question(pub_date=time)
self.assertIs(old_question.was_published_recently(), False)
def test_was_published_recently_with_recent_question(self):
"""
was_published_recently() returns True for questions whose pub_date
is within the last day.
"""
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
recent_question = Question(pub_date=time)
self.assertIs(recent_question.was_published_recently(), True)