700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python测试开发django-1.开始hello world!

python测试开发django-1.开始hello world!

时间:2020-06-01 00:21:17

相关推荐

python测试开发django-1.开始hello world!

前言

当你想走上测试开发之路,用python开发出一个web页面的时候,需要找一个支持python语言的web框架。django框架有丰富的文档和学习资料,也是非常成熟的web开发框架,想学python开发的小伙伴,从django入手是一个不错的选择。本篇写一个简单的“hello world! ”页面,开始django之旅~

环境准备:

Python 3.6.0

django 2.1.2

pycharm

环境准备

django的环境安装非常简单,只需用pip安装一个django库就可以了,编辑器选择pycharm

pip install django

查看版本号:pip show django

C:\Users\dell>pip show djangoName: DjangoVersion: 2.1.2Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.Home-page: /Author: Django Software FoundationAuthor-email: foundation@License: BSDLocation: e:\python36\lib\site-packagesRequires: pytzRequired-by:

安装完之后在cmd检查下是否能用

C:\Users\dell>pythonPython 3.6.0 (v3.6.0:41df79263a11, Dec 23 , 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import django>>> print(django.get_version())2.1.2>>>

创建项目

先建一个工程,比如我的项目代码想放到E:\web_djo目录下,然后新建一个Django project( 即一个 Django 项目实例需要的设置项集合,包括数据库配置、Django 配置和应用程序配置。)

打开命令行,cd 到一个你想放置你代码的目录,然后运行以下命令:

django-admin startproject helloworld

执行完之后打开pycharm就可以看到web_djo工程目录下多了以下层级文件

─helloworld│ manage.py│ └─helloworldsettings.pyurls.pywsgi.py__init__.py

这些目录和文件的用处是:

最外层的:helloworld: 项目的容器,可以随便命名。manage.py: 一个让你用各种方式管理 Django 项目的命令行工具。helloworld/__init__.py:一个空文件,告诉 Python 这个目录应该被认为是一个 Python 包。helloworld/settings.py:Django 项目的配置文件。helloworld/urls.py:Django 项目的 URL 声明,就像你网站的“目录”。helloworld/wsgi.py:作为你的项目的运行在 WSGI 兼容的Web服务器上的入口。

django-admin

django-admin.exe是一个可执行文件,安装django时候会默认安装到python3\Scripts目录下,相关指令用-h查看

E:\python36\Scripts>django-admin -hType 'django-admin help <subcommand>' for help on a specific subcommand.Available subcommands:[django]checkcompilemessagescreatecachetabledbshelldiffsettingsdumpdataflushinspectdbloaddatamakemessagesmakemigrationsmigraterunserversendtestemailshellshowmigrationssqlflushsqlmigratesqlsequenceresetsquashmigrationsstartappstartprojecttesttestserverNote that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).

启动服务

接下来启动django服务,使用helloworld下的manage.py,先cd到web_djo/helloworld目录下,到在命令行输入以下指令:

python manage.py runserver

E:\web_djo\helloworld>python manage.py runserverPerforming system checks...System check identified no issues (0 silenced).You have 15 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.October 23, - 21:09:39Django version 2.1.2, using settings 'helloworld.settings'Starting development server at http://127.0.0.1:8000/Quit the server with CTRL-BREAK.

启动完只看看到这句:Starting development server at http://127.0.0.1:8000/,复制地址在浏览器打开

django服务默认在8000端口启动,如果想换个端口,可以输入以下指令

python manage.py runserver 8080

如果一个局域网另外一台电脑也需要能访问,可以监听所有ip:python manage.py runserver 0.0.0.0:8000,访问的时候用电脑ip代替127.0.0.1

用于开发的服务器在需要的情况下会对每一次的访问请求重新载入一遍 Python 代码。所以你不需要为了让修改的代码生效而频繁的重新启动服务器。然而,一些动作,比如添加新文件,将不会触发自动重新加载,这时你得自己手动重启服务器。

视图和URL配置

在先前创建的helloworld/helloworld目录新建一个 view.py 文件,并输入代码

# helloworld/helloworld/view.pyfrom django.http import HttpResponsedef index(request):return HttpResponse("Hello world ! django ~~")

绑定URL与视图函数。打开 urls.py 文件,删除原来代码,将以下代码复制粘贴到 urls.py 文件中

# helloworld/helloworld/urls.pyfrom django.conf.urls import urlfrom . import viewurlpatterns = [url(r'^$', view.index),]

url函数

url() 可以接收四个参数,分别是两个必选参数:regex、view 和两个可选参数:kwargs、name.

def url(regex, view, kwargs=None, name=None):return re_path(regex, view, kwargs, name)

regex: 正则表达式,与之匹配的 URL 会执行对应的第二个参数 view。

view: 用于执行与正则表达式匹配的 URL 请求。

kwargs: 视图使用的字典类型的参数。

name: 用来反向获取 URL。

多个url设置

urlpatterns里面url(r'^$', view.index)这项是打开首页http://127.0.0.1:8000,平常网站会有多个页面,如果想加个页面地址如:http://127.0.0.1:8000/yoyo打开另外一个页面.

view.py加个函数

from django.http import HttpResponsedef index(request):return HttpResponse("Hello world ! django ~~ ")def yoyo(request):return HttpResponse("yoyo! django ~~ ")

urls.py加个配置

from django.conf.urls import urlfrom . import viewurlpatterns = [url('^$', view.index),url('^yoyo$', view.yoyo),]

这样在浏览器上输入地址:http://127.0.0.1:8000/,打开页面出现:Hello world ! django ~~

在浏览器输入地址:http://127.0.0.1:8000/yoyo, 打开页面出现:yoyo! django ~~

关于regex正则表达式用法可以参考菜鸟教程/regexp/regexp-tutorial.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。