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

现在哪个网站还做白拿百度客服电话号码

现在哪个网站还做白拿,百度客服电话号码,乐趣做网站,网络营销公司介绍官方有很详细的文档,但是看过几遍之后如果要翻找还是有点麻烦,本文算作是学习笔记,提取一些关键点记录下来,另附上官方教程 编写你的第一个 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://www.yidumall.com/news/50987.html

相关文章:

  • 中国设计人才网如何做好seo优化
  • wordpress选不了中文钦州seo
  • 南京网站制作平台上海有名网站建站开发公司
  • 制作微信网页的网站北京seo主管
  • 包装模板网站中国最新消息
  • 青岛哪家做网站好厦门关键词排名提升
  • 网至普的营销型网站建设东莞建设企业网站公司
  • 如何做英文ppt模板下载网站win10最强性能优化设置
  • 如何做亚马逊国外网站抖音seo系统
  • 网站加ico免费seo诊断
  • 重庆专业网站建设公司哪家好西安 做网站
  • php兼职平台安徽网站推广优化
  • 什么网站免费做简历模板软文怎么写吸引人
  • 无锡网站建设公司怎么样站长推荐产品
  • 为了推出企业网站建设企业文化
  • 网站域名改了帝国cms网络营销专业是做什么的
  • 网站建设服务器都有哪些淘宝seo
  • 长沙旅游攻略三日游seo网站优化培训多少价格
  • 武汉网络公司武汉做网站公司个人网站设计图片
  • 网站建设与网页设计实训报告湖南网站建设营销推广
  • 房地产交易网站模版建个网站费用大概多少钱一年
  • python可以做网站前台么专业做网络推广的公司
  • 网站建设多语种自动翻译插件教育机构加盟
  • 站长工具备案查询模板网站哪个好
  • 源服务器发生5xx错误抖音seo招商
  • 免费营销网站制作常用的搜索引擎有哪些?
  • 68Design一样设计网站济南百度推广公司电话
  • 深圳分销网站设计费用上海企业网站seo
  • 网站设计 公司 长沙百度客服人工电话24小时
  • 做一网站优化要多少钱请输入搜索关键词