Python3+Django2.0基础入门demo

 1、安装Python3环境

[root@openshift ~]# cat /etc/redhat-release

CentOS Linux release 7.4.1708 (Core)

默认为Python2.7,需要安装Python3

[root@openshift ~]#wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh

[root@openshift ~]# yum install bzip2

[root@openshift ~]# chmod +x Anaconda3-5.0.1-Linux-x86_64.sh

[root@openshift ~]# ./Anaconda3-5.0.1-Linux-x86_64.sh

执行脚本有一个交互过程,依次输入yes—“enter”按键—yes

[root@openshift ~]# source /root/.bashrc

[root@openshift ~]# python -V

Python 3.6.3 :: Anaconda, Inc.

2、安装django2.0

[root@openshift ~]# yum install python-pip

[root@openshift ~]# pip install django==2.0

3、创建项目

[root@openshift ~]# cd /root/python3pro

[root@openshift python3pro]# django-admin startproject testblog

[root@openshift python3pro]# cd testblog/

[root@openshift testblog]# ls

manage.py  testblog

[root@openshift testblog]# python manage.py startapp testapp

[root@openshift testblog]# ls

manage.py  testapp  testblog

[root@openshift testblog]#

4、配置访问基础

配置访问权限和添加app,设置settings文件,

ALLOWED_HOSTS = [‘*‘]

INSTALLED_APPS = [

‘django.contrib.admin‘,

‘testapp‘,

]

初始化数据库和后台访问账户密码

[root@openshift testblog]# python manage.py migrate

[root@openshift testblog]# python manage.py createsuperuser

Username (leave blank to use ‘root‘): sunny

Email address: 123@qq.com

Password:

Password (again):

设置账户密码 sunny test12345

5、访问Python3 Django2案例

[root@openshift testblog]# vim testapp/views.py 需要新增部分

from django.shortcuts import render

from django.http import HttpResponse

def index(request):

return HttpResponse(‘Python3 Django2‘)

[root@openshift testblog]# vim testblog/urls.py 需要新增部分

from django.contrib import admin

from django.urls import path

from testone import views

urlpatterns = [

path(‘admin/‘, admin.site.urls),

#path(‘aa/‘, views.index),

path(‘‘, views.index),

]

启动项目,

[root@openshift testblog]# python manage.py runserver 0.0.0.0:8000

访问测试,

[root@openshift ~]# curl  http://172.16.16.71:8001/

Python3 Django2

[root@openshift ~]# curl -I http://172.16.16.71:8001

HTTP/1.1 200 OK

后台访问:

6、配置mysql数据库

创建数据库

[root@openshift testblog]# mysql -h127.0.0.1 -uroot -p123456  -e ‘create database testblog character set utf8 collate utf8_bin;‘    创建testblog数据库

[root@openshift testblog]# mysql -h127.0.0.1 -uroot -p123456 -e ‘show create database testblog‘

+----------+------------------------------------------------------------------------------------+

| Database | Create Database                                                                    |

+----------+------------------------------------------------------------------------------------+

| testblog | CREATE DATABASE `testblog` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */ |

+----------+------------------------------------------------------------------------------------+

配置数据库文件

Settings.py配置数据库

安装mysql数据库驱动

[root@openshift testblog]# pip install mysql-python

[root@openshift testblog]# pip install PyMySQL

删除db.sqlite数据,启用mysql数据库

[root@openshift testblog]# python manage.py migrate

[root@openshift testblog]# python manage.py createsuperuser

[root@openshift testblog]# python manage.py runserver 0.0.0.0:8001

查看生成的数据

[root@openshift]# mysql -h127.0.0.1 -uroot -p123456 -e ‘use testblog; show tables‘

+----------------------------+

| Tables_in_testblog         |

+----------------------------+

| auth_group                 |

| ......           |

| django_migrations          |

| django_session             |

+----------------------------+

自此,完成了Python3+Django2.0环境初始化部署。

7、配置静态html页面

配置settings.py文件,

补充DIRS路径,末尾新增两行。

STATIC_URL = ‘/static/‘

TEMPLATE_DIRS  = [ os.path.join(BASE_DIR, "templates"),]

STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"),]

配置视图

[root@openshift testblog]# vim testapp/views.py

from django.shortcuts import render

from django.http import HttpResponse

def index(request):

return render(request,‘article.html‘)

配置url,跟上述一致,不需要改变。

配置html文件,目录结构如下,

[root@openshift testblog]# cat templates/article.html

<!DOCTYPE>

<html>

<head>

<title>Django2.0测试</title>

<link href="/static/css/base.css" rel="stylesheet" type="text/css"/>

</head>

<body>

<h2 class="headtop">使用 vue 开发者工具调试</h2>

<ul class="content">

<li>1、遵循 MVVM 模式</li>

<li>2、编码简洁, 体积小, 运行效率高, 适合移动/PC 端开发</li>

<li>3、只关注 UI,可轻松引入 vue 插件或其它第三库开发项目</li>

</ul>

<div class="footer"><img src="/static/images/test.png"/></div>

</body>

</html>

[root@openshift testblog]#

[root@openshift testblog]# ls static/images/test.png

static/images/test.png

[root@openshift testblog]# cat static/css/base.css

.headtop{

height:60px;

line-height:60px;

text-align:center;

background:#e8e8e8;

}

.content{list-style-type :none;}

.content li {background:#fafafa;margin: 10px;}

.footer{margin-left: 100px;}

.footer img{width: 200px; height:100px;}

[root@openshift testblog]#

展示效果如下,

自此,完成静态页面部署工作。

8、简易博客系统部署

设计表,定义model

[root@openshift testblog]# vim testapp/models.py

from django.db import models

class BlogsPost(models.Model):

title = models.CharField(max_length = 150)

body = models.TextField()

timestamp = models.DateTimeField()

执行数据库同步

[root@openshift testblog]# python manage.py makemigrations testapp

Did you rename the testapp.BlogsPost model to Bloglists? [y/N] Y

Migrations for ‘testapp‘:

testapp/migrations/0002_auto_20190213_0405.py

- Rename model BlogsPost to Bloglists

[root@openshift testblog]#

通过后台admin管理,

[root@openshift testblog]# vim testapp/admin.py

from django.contrib import admin

from testapp.models import Bloglists

class BloglistsAdmin(admin.ModelAdmin):

list_display = [‘title‘, ‘body‘, ‘timestamp‘]

admin.site.register(Bloglists, BloglistsAdmin)

访问登录admin后台,

界面操作后,保存,数据存入数据库。

[root@openshift ~]# mysql -h127.0.0.1 -uroot -p123456 -e ‘use testblog; select * from testapp_bloglists‘

+----+--------------+------------------------------------------+---------------------+

| id | title        | body                                     | timestamp           |

+----+--------------+------------------------------------------+---------------------+

|  1 | test         | test湖北武汉市光谷软件园二期             | 2019-02-13 04:15:26 |

|  2 | 第二节课     | 熟悉掌握Django2.0使用方法                | 2019-02-13 04:30:54 |

+----+--------------+------------------------------------------+---------------------+

[root@openshift ~]#

前端展示数据

创建视图

[root@openshift testblog]# vim testapp/views.py

from django.shortcuts import render

from django.http import HttpResponse

from testapp.models import Bloglists

def blog_index(request):

blog_list = Bloglists.objects.all()

return render(request,‘blog.html‘, {‘blog_list‘:blog_list})

创建url地址,

[root@openshift testblog]# vim testblog/urls.py

from testapp import views

urlpatterns = [

path(‘admin/‘, admin.site.urls),

path(‘‘, views.index),

path(‘blog/‘, views.blog_index),

]

创建模板,

[root@openshift testblog]# vim templates/blog.html

{% for blog in blog_list %}

<h2>{{ blog.title }}</h2>

<p>{{ blog.body }}</p>

<p>{{ blog.timestamp }}</p>

{% endfor %}

访问链接:

可以通过引入样式文件优化界面展示效果,自行优化。

自此完成了整个Python3+Django2.0项目部署整个过程。

原文地址:https://www.cnblogs.com/sunnyyangwang/p/10372385.html

时间: 2024-08-02 09:09:39

Python3+Django2.0基础入门demo的相关文章

饥荒MOD lua编程0基础入门

前言 原贴写于饥荒游戏贴吧,为了使文章针对性更强,将原文切割并精简.此贴主要为编程0基础的modder讲解一些编程的基础知识.至于说有关饥荒框架的介绍,则会放在另一篇文章里讲解. 编程0基础的人,要想学习制作MOD,难度是比较大的,因为缺乏一些基本的编程概念,只懂得复制别人的代码或者在它们的基础上稍加改变,遇到稍微复杂一点的代码,就束手无策了.对于MOD崩溃或错误,也几乎没办法自行处理.但我也不推荐先去学一门编程语言之后再来学习MOD代码,这是没有必要的.事实上饥荒MOD里用到的基本编程知识都比

八年一线架构师,带你0基础入门大数据

在职八年老司机带你0基础入门大数据 ,教你如何从小白变成行业精英 ,让高薪变的简单! 孙老师太阁孙老师具备8年从业经验,4年大数据经验,4年培训讲师经验,精通java python 和大数据生态圈,曾担任清华大学JAVA技术研究与开发联合实验室研究员,设计过滴滴大数据架构,以及国家级项目,对于数据的处理和分析有独到的见解,对于教学能够如浅入深,有丰富的软件设计,软件研发,软件管理,流程控制经验点击进入课程 官方网址:www.tigerlab.net太阁博客:blog.tigerlab.net官方

python3.5+django2.0快速入门(一)

因为这篇教程需要用到anaconda的一些操作,如果还不懂anaconda的操作的同学可以看下这篇文章python 入门学习之anaconda篇. 创建python3+的开发环境 直接在终端输入:conda create -n newenv python=3.5 进入我们新建的开发环境newenv linux: source activate newenv window: activate newenv 安装django2.0 直接在终端输入: pip install django ,然后系统会

Python 3之Django2部署(centos7+nginx+python3+django2.0)

前置工具,系统为centos7.5,为了方便管理,可以安装宝塔免费版本 首先, yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && bash install.sh 正常干净系统会顺利完成安装,使用过程是感觉自己记下配置和更新的过程,类似版本更新或者冲突的先别急着百度,容易造成冲突. 宝塔6.6版本是依赖python2.7的,所以python可以升级

使用python3+django2.0 版本的注意事项:

使用 Python3.6+django1.11不会出现问题 基于 python3.6+django1.11的基础上升级 原文地址:https://www.cnblogs.com/polite/p/10339990.html

shell总结(0基础入门)

一.简介 shell是用户和操作系统交互的命令行解释器. shell有很多种: bash.csh.sh.ksh... 我们等了linux时看到的命令行就是一个bash. 二.第一个脚本: [[email protected] script]# vim first.sh #!/bin/bash#auther:xiaofan#time:2016.10.4#井号是注释echo "hello word" 执行脚本: 方法1: [[email protected] script]# bash f

django2.0基础

一.安装与项目的创建 1.安装 pip install django 2.查看版本 python -m django --version 3.创建项目 django-admin startproject mysite manage.py 实用的与django项目进行交互的命令行工具 mysite 项目中的实际python包 mysite/__init__.py 空文件,表示这是一个python包 mysite/settings.py 此项目的配置文件 mysite/urls.py url声明文件

python3完全零基础入门视频

[课程内容] 计算机基础常识Python语言概览.安装与运行Python 变量.数据类型及存储Python 常用数据类型概览数值与字符串列表list字典表dict 与元组 tuple文件与类型汇总语句.表达式与if分支循环语句迭代函数定义与参数函数与Lambda表达式函数应用与工具包与模块管理及面向对象初步面向对象编程OOP异常处理及测试unittest单元测试数值.日期与时间对象持久化字符与编码正则表达式系统编程python并行编程函数高级应用及装饰器Python-SublimeText-Py

[00014]-[2015-09-04]-[00]-[WinSocket编程0 基础入门]

[网络程序开发]---> 基于局域网或互联网,利用网络开发技术,开发能够运行在网络上的软件(系统) [网络程序结构]---> C/S B/S [C/S] Client/Server 即客户端与服务端结构,Client将用户的需求提交给Server,再将Server返回的结果以一定的形式提供给用户,  Server的任务就是接受Client提出的服务请求,进行相应的处理,并将结果返回给Client. 在C/S结构下,服务器程序通常在  一个固定的地址(IP&Port)监听客户的请求,服务