Django Web开发入门基础

官方有很详细的文档,但是看过几遍之后如果要翻找还是有点麻烦,本文算作是学习笔记,提取一些关键点记录下来,另附上官方教程 编写你的第一个 Django 应用

注: 文中的指令使用py,是在Windows上,macOS要使用 python3

 1. 安装Django

Django 是一个基于 Python 的Web开发框架,安装前可以用下面的命令检查是否安装了 Django,如果已经安装,会显示版本,没有安装会提示没有该模块

py -m django --version

如果没有安装,可以使用下面的命令安装

py -m pip install Django

2. 创建项目

项目就是一个 project,一个项目可以包含多个 app,也可以理解为多个模块,创建项目使用如下命令,其中 mysite 是项目名称:

django-admin startproject mysite

在令窗口中,切到刚刚创建的项目的根目录(有 manage.py),在项目中创建一个app,名字就叫 polls

py manage.py startapp polls

 然后修改几个文件

polls/views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello World!")

polls/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path("", views.index, name="index"),
]

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),
]

 最后启动服务,浏览器访问 http://localhost:8000/polls/

py manage.py runserver

3. 配置语言和时区 

项目配置在 mysite/settings.py

语言标识可以在这里查找 Language Identifiers (RFC 3066)

# 语言设置为中文
LANGUAGE_CODE = 'zh-Hans'

时区标识可以在这里查找 List of tz database time zones 

# 时区设置为北京时间
TIME_ZONE = 'Asia/Shanghai'

4. 数据库

就使用自带的 SQLite 吧,方便又简单,如果开发商用的Web App,建议使用其他数据库,要进行额外的配置。

(1)创建数据模型

修改 polls/models.py

import datetime

from django.db import models
from django.utils import timezone

class Question(models.Model):
	question_text = models.CharField(max_length=200)
	pub_date = models.DateTimeField("date published")

	def __str__(self):
		return self.question_text

	def was_published_recently(self):
		return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):
	# Choice属于一个Question
	question = models.ForeignKey(Question, on_delete=models.CASCADE)
	choice_text = models.CharField(max_length=200)
	votes = models.IntegerField(default=0)

	def __str__(self):
		return self.choice_text

(2)激活模型

修改 mysite/settings.py,在 INSTALLED_APP 中添加 polls.apps.PollsConfig

INSTALLED_APPS = [
    "polls.apps.PollsConfig",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
]

(3)创建数据迁移(尚未提交)

py manage.py makemigrations polls

查看创建的迁移使用的SQL语句,下面命令中的 0001 是上面创建迁移时生成的 polls/migrations/0001_initial.py 文件名中的数字

py manage.py sqlmigrate polls 0001

(4)提交迁移

py manage.py migrate

总结起来就三步:修改模型 -> 创建迁移 -> 提交迁移

 (5)玩转数据库API

打开交互式窗口

py manage.py shell
>>> from polls.models import Choice, Question
>>> Question.objects.all()

>>> from django.utils import timezone
>>> q = Question(question_text="What's new?", pub_date=timezone.now())
>>> q.save()
>>> q.id
1
>>> q.question_text
"What's new?"
>>> q.pub_date
datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=datetime.timezone.utc)
>>> q.question_text = "What's up?"
>>> q.save()
>>> Question.objects.all()
]>
>>> from polls.models import Choice, Question
>>> Question.objects.all()
]>
>>> Question.objects.filter(id=1)
]>
>>> Question.objects.filter(question_text__startswith="What")
]>
>>> from django.utils import timezone
>>> current_year = timezone.now().year
>>> Question.objects.get(pub_date__year=current_year)

>>> Question.objects.get(id=2)
Traceback (most recent call last):
    ...
DoesNotExist: Question matching query does not exist.
>>> Question.objects.get(pk=1)

>>> q = Question.objects.get(pk=1)
>>> q.was_published_recently()
True
>>> q = Question.objects.get(pk=1)
>>> q.choice_set.all()

>>> q.choice_set.create(choice_text="Not much", votes=0)

>>> q.choice_set.create(choice_text="The sky", votes=0)

>>> c = q.choice_set.create(choice_text="Just hacking again", votes=0)
>>> c.question


>>> q.choice_set.all()
, , ]>
>>> q.choice_set.count()
3
>>> Choice.objects.filter(question__pub_date__year=current_year)
, , ]>
>>> c = q.choice_set.filter(choice_text__startswith="Just hacking")
>>> c.delete()

5. 创建管理员

 输入下面的命令,然后根据提示设置账户名,邮箱,密码

py manage.py createsuperuser

启动服务,浏览器访问 http://127.0.0.1:8000/admin/

py manage.py runserver

Django Web开发入门基础_第1张图片

修改 polls/admin.py 让管理员可管理数据

from django.contrib import admin

from .models import Question

admin.site.register(Question)

管理员登录后

Django Web开发入门基础_第2张图片

6. 使用 html 模板

(1)创建 HTML 文件

在 polls/ 目录下新建一个 templates 文件夹,在 polls\templates\ 目录下新建一个 polls 文件见,如果新建一个 HTML 文件,比如 index.html,其最终的路径为 mysite\polls\templates\polls\index.html,代码中将使用 polls\index.html

polls\templates\polls\index.html

{% if latest_question_list %}
    
{% else %}
    

No polls are available.

{% endif %}

 polls\templates\polls\detail.html

{{ question.question_text }}

    {% for choice in question.choice_set.all %}
  • {{ choice.choice_text }}
  • {% endfor %}

(2)配置 view

修改 polls/views.py

from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404

from .models import Question

def index(request):
	latest_question_list = Question.objects.order_by("-pub_date")[:5]
	context = {"latest_question_list": latest_question_list}
	return render(request, "polls/index.html", context)

def detail(request, question_id):
	question = get_object_or_404(Question, pk=question_id)
	context = {"question": question}
	return render(request, "polls/detail.html", context)

def results(request, question_id):
	return HttpResponse("Results")

def vote(request, question_id):
	return HttpResponse("Vote")

(3)配置 url

修改 polls/urls.py

from django.urls import path

from . import views

urlpatterns = [
    # ex: /polls/
    path("", views.index, name="index"),
    # ex: /polls/5/
    path("/", views.detail, name="detail"),
    # ex: /polls/5/results/
    path("/results/", views.results, name="results"),
    # ex: /polls/5/vote/
    path("/vote/", views.vote, name="vote"),
]

(4)URL命名空间

当项目存在多个应用时,可能需要借助命名空间来确定url,在应用的 urls.py 中配置 app_name

from django.urls import path

from . import views

app_name = "polls"
urlpatterns = [
    # ex: /polls/
    path("", views.index, name="index"),
    # ex: /polls/5/
    path("/", views.detail, name="detail"),
    # ex: /polls/5/results/
    path("/results/", views.results, name="results"),
    # ex: /polls/5/vote/
    path("/vote/", views.vote, name="vote"),
]

index.html 中添加 app_name:

{% if latest_question_list %}
    
{% else %}
    

No polls are available.

{% endif %}

(5)创建表单

 修改 detail.html

{% csrf_token %}

{{ question.question_text }}

{% if error_message %}

{{ error_message }}

{% endif %} {% for choice in question.choice_set.all %}
{% endfor %}

表单中涉及投票页面和投票结果页面,得新增一个 results.html,并修改 views.py

results.html

{{ question.question_text }}

    {% for choice in question.choice_set.all %}
  • {{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}
  • {% endfor %}
Vote again?

views.py 中修改 results 和 vote 方法

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.urls import reverse

from .models import Question, Choice

def index(request):
	latest_question_list = Question.objects.order_by("-pub_date")[:5]
	context = {"latest_question_list": latest_question_list}
	return render(request, "polls/index.html", context)


def detail(request, question_id):
	question = get_object_or_404(Question, pk=question_id)
	context = {"question": question}
	return render(request, "polls/detail.html", context)


def results(request, question_id):
	question = get_object_or_404(Question, pk=question_id)
	return render(request, "polls/results.html", {"question": question})


def vote(request, question_id):
	question = get_object_or_404(Question, pk=question_id)
	try:
		selected_choice = question.choice_set.get(pk=request.POST["choice"])
	except (KeyError, Choice.DoesNotExist):
		return render(
			request, 
			"polls/detail.html",
			{
				"question": question,
				"error_message": "You didn't select a choice."
			},
		)
	else:
		selected_choice.votes += 1
		selected_choice.save()
		return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))

7. 编写测试用例 

首先得有个bug,巧了,我们的 polls 应用现在就有一个小 bug 需要被修复:我们的要求是如果 Question 是在一天之内发布的, Question.was_published_recently() 方法将会返回 True ,然而现在这个方法在 Question 的 pub_date 字段比当前时间还晚时也会返回 True(这是个 Bug)。

 在 polls\tests.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):
		"""
		检测异常:发布时间比当前时间还要晚
		"""
		time = timezone.now() + datetime.timedelta(days=30)
		future_question = Question(pub_date=time)
		self.assertIs(future_question.was_published_recently(), False)

运行测试(指令是在项目跟目录执行的,该目录有 manage.py)

py manage.py test polls
  • py manage.py test polls 将会寻找 polls 应用里的测试代码
  • 查找 django.test.TestCase 子类
  • 创建一个特殊的数据库供测试使用
  • 在类中寻找测试方法——以 test 开头的方法。

你可能感兴趣的:(Python,django,python,后端)