1.安装软件
VMware搭建虚拟环境
安装依赖(提供的系统安装包内已经准备好)
安装python-pip(pip是一个专门管理python包的工具,它可以快速安装、卸载python的包)
- virtualenv+virtualenvwrapper(python系统环境管理工具)
- django1.11
2.Xshell 连接linux
安装django 1.11
pip install django==1.11 django==1.11 -i https://pypi.douban.com/simple/
3.pychan 连接Linux
4.开始运行项目
#创建项目:
django-admin startproject Myreport
#创建APP
python manage.py startapp hello
#运行
python manage.py runserver 0.0.0.0:8080
如果连接不上,应该是防火墙的问题
查看防火墙状态:
firewall-cmd --state
停止firewall
systemctl stop firewalld.service
启动firewall
systemctl start firewalld.service
禁止firewall开机启动
systemctl disable firewalld.service
5.urls 和views
# urls
from django.conf.urls import url
from django.contrib import admin
from hello import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/', views.Hello.as_view())
]
#views
from __future__ import unicode_literals
from django.shortcuts import render, redirect
from django.views import View
from django.http import HttpResponse
# Create your views here.
class Hello(View):
def get(self,request):
mess = "hello zzz"
return HttpResponse(mess)
打开结果如下:
image.png
URL
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/2003/$', views.ArticlesYear.as_view()), #普通用法
url(r'^articles/([0-9]{4})/$', views.ArticlesMonth.as_view()), # 单个非命名参数
url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.ArticlesActive.as_view()),
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.ArticlesAll.as_view()),
]
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/2003/$', views.ArticlesYear.as_view()),
url(r'^articles/(?P<year>[0-9]{4})/$', views.ArticlesMonth.as_view() ),
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.ArticlesActive.as_view()),
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.ArticlesAll.as_view()),
]
普通视图的写法和URL写法
#URL
url(r'^hello/$', views.Hello.as_view()),
url(r'^hello/[0-9]{4}/$', views.Hello.as_view()),
#view
class Hello(View):
def get(self,request):
mess = "hello zzz"
return HttpResponse(mess)
非命名URL写法
#url
url(r'^hello2/([0-9]{4})/$', views.Hello2.as_view()),
url(r'^hello2/([0-9]{4})/([0-9]{2})/$', views.Hello3.as_view()),
#view
class Hello2(View):
def get(self,request,year):
mess = "hello zzz\t" + year
return HttpResponse(mess)
class Hello3(View):
def get(self,request,year,month):
mess = "hello zzz\t%s\t%s" %(year,month)
return HttpResponse(mess)
命名URL写法
url(r'^hello4/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.Hello4.as_view()),
class Hello4(View):
def get(self,request,year,month):
mess = "hello zzz\t%s\t%s" %(year,month)
return HttpResponse(mess)
#这样写OK:
def get(self,request,year,month):
def get(self,request,year=1,month=1):
def get(self,request,year,month=1):
#这样写不行
def get(self,request,year=1,month):
url分组,便于管理
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
# namespace只有在有多个include时,才需要
# namespace 与name的设置规则一样,同一级别,名称需要保持唯一
url(r'^hello/', include("hello.urls", namespace="hello")),
]
#另一个urls
urlpatterns = [
url(r'^login123/$', views.Hello.as_view(), name="login"),
url(r'^articles/2003/$', views.Hello.as_view()), # 普通url写法
url(r'^articles/[a-z]{4}/$', views.Hello.as_view()), # 普通url 带正则写法
url(r'^articles/(abcd)', views.Hello1.as_view()),
# url(r'^articles/([0-9]{4})/$', views.Hello1.as_view()), # 单个带非命名参数的写法,
url(r'^articles/([0-9]{4})/([0-9]{2})([0-9]{2})/$', views.Hello2.as_view(), name="articles"), # 多个带非命名参数的写法
url(r'^hello3/(?P<year>[0-9]{4})/', views.Hello3.as_view(), name="hello3"), #单个命名参数
url(r'^hello5/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/', views.Hello5.as_view()), #多个命名参数
url(r'^hello4/(?P<year>[a-z]{4})/', views.hello4), #单个命名参数
url(r'^error500/$',views.Error500.as_view()),
url(r"redirct/(?P<year>[0-9]{1})/", views.RedicectPage.as_view())
]
URL 的反向解析
#在views 添加
from django.core.urlresolvers import reverse
url(r'^login/$', views.Login.as_view(), name = "login"),
class RedicectPage(View):
def get(self, request,year):
if year == '1':
return redirect("/hello/hello") #OK
return redirect("login") #OK
return redirect(reverse("login")) #OK
else:
return HttpResponse("登陆成功")
class RedicectPage(View):
def get(self, request, year="1"):
# 这是一个重定向方法
# 我如果需要在这里做一个跳转
if year == "1":
# reverse
# 未带参数
return redirect(reverse("login"))
# 带非命名参数的跳转
return redirect(reverse("articles", args=("2018", "03","21")))
# 命名参数的跳转
return redirect(reverse("hello3", kwargs={"year": 2000}))
# 如果name是唯一的时候,你可以直接只使用name
# 但是如果name在其它include中也存在相同name,为了区分,我们可以给inlude设置一个namespace
# 我们通过命名空间名称+名称访问
# 为了程序的健壮性,我们尽量使用命名空间+名称访问
# 顶层其实有一个app名称,但是我们日常开发中,几乎不使用,该功能了解知道即可
return redirect(reverse("hello:login"))
else:
return HttpResponse("登录成功")
模板渲染
#首先,先写html
#第一种:reander
#render 目标渲染
#redirect 重定向
from django.shortcuts import render, redirect
class Reader_template(View):
# render渲染H5
def get(self,request):
# render最少需要两个参数
# 第一个为request, 它是get方法默认自带的参数
# 第二个为模版路径
return render(request,"reader_templates.html")
#第二种:get_template
from django.template.loader import get_template
class Get_template(View):
# get_template渲染H5
def get(self,request):
template = get_template("reader_templates.html")
# template实际是一个对象
return HttpResponse(template.render())
动态渲染
class RenderParam(View):
# 动态渲染参数
def get(self, request):
# global message
mess = "大家好"
date = "今天是星期五,明天后天就不上班啊。"
# 传递一个参数给前端页面
# 配置一个字典参数传递, 它的键名,为模版中的参数名字,值为具体内容
# return render(request, "render_param.html", context={"mess": message})
# 如果有多个参数
# return render(request, "render_param.html", context={"mess": message, "date": date})
# 如果参数较多,不想一个一个写,可以使用locals
# locals()函数会把当前能访问到的局部变量,转换成一个字典
print locals()
# return render(request, "render_param.html", context=locals())
# 我们平时content可以不写
# 这里的参数,html中不一定要使用
return render(request, "render_param.html", locals())
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ mess }}
</body>
</html>
网友评论