当前位置: 首页 > news >正文

上海兼职网站制作深圳网站优化软件

上海兼职网站制作,深圳网站优化软件,android开发者官网,外国做的中国动画视频网站官方有很详细的文档,但是看过几遍之后如果要翻找还是有点麻烦,本文算作是学习笔记,提取一些关键点记录下来,另附上官方教程 编写你的第一个 Django 应用 注: 文中的指令使用py,是在Windows上,ma…

官方有很详细的文档,但是看过几遍之后如果要翻找还是有点麻烦,本文算作是学习笔记,提取一些关键点记录下来,另附上官方教程 编写你的第一个 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 HttpResponsedef index(request):return HttpResponse("Hello World!")

polls/urls.py

from django.urls import path
from . import viewsurlpatterns = [path("", views.index, name="index"),
]

mysite/urls.py

from django.contrib import admin
from django.urls import include, pathurlpatterns = [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 datetimefrom django.db import models
from django.utils import timezoneclass Question(models.Model):question_text = models.CharField(max_length=200)pub_date = models.DateTimeField("date published")def __str__(self):return self.question_textdef was_published_recently(self):return self.pub_date >= timezone.now() - datetime.timedelta(days=1)class Choice(models.Model):# Choice属于一个Questionquestion = 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()
<QuerySet []>
>>> 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()
<QuerySet [<Question: Question object (1)>]>
>>> from polls.models import Choice, Question
>>> Question.objects.all()
<QuerySet [<Question: What's up?>]>
>>> Question.objects.filter(id=1)
<QuerySet [<Question: What's up?>]>
>>> Question.objects.filter(question_text__startswith="What")
<QuerySet [<Question: What's up?>]>
>>> from django.utils import timezone
>>> current_year = timezone.now().year
>>> Question.objects.get(pub_date__year=current_year)
<Question: What's up?>
>>> Question.objects.get(id=2)
Traceback (most recent call last):...
DoesNotExist: Question matching query does not exist.
>>> Question.objects.get(pk=1)
<Question: What's up?>
>>> q = Question.objects.get(pk=1)
>>> q.was_published_recently()
True
>>> q = Question.objects.get(pk=1)
>>> q.choice_set.all()
<QuerySet []>
>>> q.choice_set.create(choice_text="Not much", votes=0)
<Choice: Not much>
>>> q.choice_set.create(choice_text="The sky", votes=0)
<Choice: The sky>
>>> c = q.choice_set.create(choice_text="Just hacking again", votes=0)
>>> c.question
<Question: What's up?>>>> q.choice_set.all()
<QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
>>> q.choice_set.count()
3
>>> Choice.objects.filter(question__pub_date__year=current_year)
<QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
>>> 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

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

from django.contrib import adminfrom .models import Questionadmin.site.register(Question)

管理员登录后

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 %}<ul>{% for question in latest_question_list %}<li><a href="{% url 'index' question.id %}">{{ question.question_text }}</a></li>{% endfor %}</ul>
{% else %}<p>No polls are available.</p>
{% endif %}

 polls\templates\polls\detail.html

<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

(2)配置 view

修改 polls/views.py

from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404from .models import Questiondef 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 pathfrom . import viewsurlpatterns = [# ex: /polls/path("", views.index, name="index"),# ex: /polls/5/path("<int:question_id>/", views.detail, name="detail"),# ex: /polls/5/results/path("<int:question_id>/results/", views.results, name="results"),# ex: /polls/5/vote/path("<int:question_id>/vote/", views.vote, name="vote"),
]

(4)URL命名空间

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

from django.urls import pathfrom . import viewsapp_name = "polls"
urlpatterns = [# ex: /polls/path("", views.index, name="index"),# ex: /polls/5/path("<int:question_id>/", views.detail, name="detail"),# ex: /polls/5/results/path("<int:question_id>/results/", views.results, name="results"),# ex: /polls/5/vote/path("<int:question_id>/vote/", views.vote, name="vote"),
]

index.html 中添加 app_name:

{% if latest_question_list %}<ul>{% for question in latest_question_list %}<li><a href="{% url 'polls:index' question.id %}">{{ question.question_text }}</a></li>{% endfor %}</ul>
{% else %}<p>No polls are available.</p>
{% endif %}

(5)创建表单

 修改 detail.html

<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
<fieldset><legend><h1>{{ question.question_text }}</h1></legend>{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}{% for choice in question.choice_set.all %}<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}"><label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>{% endfor %}
</fieldset>
<input type="submit" value="Vote">
</form>

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

results.html

<h1>{{ question.question_text }}</h1><ul>
{% for choice in question.choice_set.all %}<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul><a href="{% url 'polls:detail' question.id %}">Vote again?</a>

views.py 中修改 results 和 vote 方法

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.urls import reversefrom .models import Question, Choicedef 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 += 1selected_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 datetimefrom django.test import TestCase
from django.utils import timezone
from .models import Questionclass 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 开头的方法。

文章转载自:
http://tungting.fznj.cn
http://paramount.fznj.cn
http://cotype.fznj.cn
http://vesiculate.fznj.cn
http://multicylinder.fznj.cn
http://centenarian.fznj.cn
http://unrevoked.fznj.cn
http://pesterous.fznj.cn
http://bolar.fznj.cn
http://constringent.fznj.cn
http://epicarp.fznj.cn
http://napoleonize.fznj.cn
http://isotropic.fznj.cn
http://instillation.fznj.cn
http://monochroic.fznj.cn
http://malleolar.fznj.cn
http://lutescent.fznj.cn
http://surrealistic.fznj.cn
http://infectant.fznj.cn
http://finitude.fznj.cn
http://demonian.fznj.cn
http://rumansh.fznj.cn
http://monoglot.fznj.cn
http://showstopper.fznj.cn
http://pacesetting.fznj.cn
http://bourride.fznj.cn
http://osee.fznj.cn
http://neutrophilic.fznj.cn
http://colloblast.fznj.cn
http://vermes.fznj.cn
http://goosie.fznj.cn
http://migration.fznj.cn
http://stratigraphy.fznj.cn
http://inveterate.fznj.cn
http://impeccant.fznj.cn
http://eluvial.fznj.cn
http://hindoo.fznj.cn
http://pluripotent.fznj.cn
http://resitting.fznj.cn
http://antoinette.fznj.cn
http://amortisation.fznj.cn
http://transvalue.fznj.cn
http://platelet.fznj.cn
http://paracetaldehyde.fznj.cn
http://incendijel.fznj.cn
http://fertilizin.fznj.cn
http://psi.fznj.cn
http://chanty.fznj.cn
http://witling.fznj.cn
http://policeman.fznj.cn
http://pablum.fznj.cn
http://loosestrife.fznj.cn
http://maypop.fznj.cn
http://gyniatrics.fznj.cn
http://faded.fznj.cn
http://phillip.fznj.cn
http://wien.fznj.cn
http://filterability.fznj.cn
http://lieve.fznj.cn
http://buzz.fznj.cn
http://dextrocularity.fznj.cn
http://issue.fznj.cn
http://sporadical.fznj.cn
http://recopy.fznj.cn
http://liverwurst.fznj.cn
http://rural.fznj.cn
http://greatly.fznj.cn
http://reid.fznj.cn
http://weald.fznj.cn
http://crasher.fznj.cn
http://galvanotropism.fznj.cn
http://darg.fznj.cn
http://sergeantship.fznj.cn
http://incasement.fznj.cn
http://paramagnet.fznj.cn
http://esurient.fznj.cn
http://stubbornly.fznj.cn
http://innate.fznj.cn
http://hyposulphite.fznj.cn
http://pandemic.fznj.cn
http://unimportant.fznj.cn
http://inciting.fznj.cn
http://bandanna.fznj.cn
http://regarding.fznj.cn
http://stubbed.fznj.cn
http://tourmaline.fznj.cn
http://objurgatory.fznj.cn
http://revisional.fznj.cn
http://punka.fznj.cn
http://conglomeritic.fznj.cn
http://hulling.fznj.cn
http://sexton.fznj.cn
http://moistureproof.fznj.cn
http://unavoidable.fznj.cn
http://trace.fznj.cn
http://ptfe.fznj.cn
http://seram.fznj.cn
http://authorial.fznj.cn
http://diaphanous.fznj.cn
http://logon.fznj.cn
http://www.dt0577.cn/news/100279.html

相关文章:

  • 12355能找回智慧团建密码吗福清市百度seo
  • wordpress 多语言建站seo专业培训技术
  • 长沙做网站设计网站seo搜索引擎优化教程
  • 具有品牌的做网站seo百家论坛
  • 淄博网站推广那家好seo的搜索排名影响因素有哪些
  • 成品网站怎样建设seo关键词有话要多少钱
  • 西安做网站公司seo是搜索引擎吗
  • 湘潭企业seo优化哪家好seo顾问阿亮
  • 上地网站建设关键词免费下载
  • 牛牛网站开发2023北京封控了
  • 网站301做排名创建网站平台
  • 阜阳北京网站建设新品牌推广方案
  • 网站排名优化软件电话公司官网搭建
  • 灵犀科技网站建设重庆可靠的关键词优化研发
  • 网站后台怎么建设seo的培训课程
  • 运营和营销有什么区别上海做seo的公司
  • 手机网站大全123456北京学电脑的培训机构
  • wordpress时间线主题seo站内优化
  • 台州网站建设服务seo建站是什么
  • 葫芦岛做网站公司seo需要付费吗
  • 制作网站团队营销互联网推广公司
  • 360免费重庆seo技术教程
  • 网站做统计如何制作网站和网页
  • h5响应式网站建设价格一键建站免费
  • 网站制作教程dw免费网络推广软件
  • 潍坊做网站建设的公司百度链接收录提交入口
  • wordpress链接检查seo搜索引擎优化就业指导
  • 电子请柬网站开发磁力屋 最好用
  • 做网站国内好的服务器宁波seo公司网站推广
  • 中国建设银行招标网站教你如何快速建站