zoukankan      html  css  js  c++  java
  • 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==2.1.2

    查看版本号:pip show django

    C:Usersdell>pip show django
    Name: Django
    Version: 2.1.2
    Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
    Home-page: https://www.djangoproject.com/
    Author: Django Software Foundation
    Author-email: foundation@djangoproject.com
    License: BSD
    Location: e:python36libsite-packages
    Requires: pytz
    Required-by:
    

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

    C:Usersdell>python
    Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
    Type "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
        │  
        └─helloworld
                settings.py
                urls.py
                wsgi.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时候会默认安装到python3Scripts目录下,相关指令用-h查看

    E:python36Scripts>django-admin -h
    
    Type 'django-admin help <subcommand>' for help on a specific subcommand.
    
    Available subcommands:
    
    [django]
        check
        compilemessages
        createcachetable
        dbshell
        diffsettings
        dumpdata
        flush
        inspectdb
        loaddata
        makemessages
        makemigrations
        migrate
        runserver
        sendtestemail
        shell
        showmigrations
        sqlflush
        sqlmigrate
        sqlsequencereset
        squashmigrations
        startapp
        startproject
        test
        testserver
    Note 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_djohelloworld>python manage.py runserver
    Performing 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, 2018 - 21:09:39
    Django 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.py
    from django.http import HttpResponse
     
    def index(request):
        return HttpResponse("Hello world !  django ~~")
    

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

    # helloworld/helloworld/urls.py
    from django.conf.urls import url
    from . import view
    
    urlpatterns = [
        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 HttpResponse
     
    def index(request):
        return HttpResponse("Hello world ! django ~~ ")
    
    
    def yoyo(request):
        return HttpResponse("yoyo!  django ~~ ")
    
    

    urls.py加个配置

    from django.conf.urls import url
    from . import view
    
    urlpatterns = [
        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正则表达式用法可以参考菜鸟教程http://www.runoob.com/regexp/regexp-tutorial.html

    django交流QQ群:779429633

  • 相关阅读:
    SQL Server 2000 JDBC驱动的完整安装及测试说明
    Google 免费:搜索本站内容
    阳光下旅行
    佛说五百年的回眸才换来今生的擦肩而过
    在程序中调用“数据链接属性”对话框,产生连接字符串
    ╭☆难以割舍^_^就是牵挂☆╮
    java打包详解
    .NET 自定义实体类简介
    C#中方法的参数有四种类型
    DataSet操作数据库
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/9839698.html
Copyright © 2011-2022 走看看