S14day19---->urls.py
from django.contrib import admin
from django.conf.urls import url,include
urlpatterns = [
url(r'^cmdb/',include('app01.urls')),
url(r'^monitor/',include('app02.urls')),
]
'''
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^index999999999/(d+)/(d+)/', views.index,name='indexx'),
url(r'^index999999999/(?P<nid>d+)/(?P<uid>d+)/', views.index,name='indexx'),
url(r'^login/',views.login),
url(r'^home/',views.Home.as_view()),
# url(r'^detail/',views.detail),
# url(r'^detail-(d+).html',views.detail),
# url(r'^detail-(d+)-(d+).html',views.detail),
# url(r'^detail-(?P<nid>d+)-(?P<uid>d+).html',views.detail),
url(r'^detail-(?P<nid>d+).html',views.detail),
]
'''
# url(r'^detail-(d+).html',views.detail), 这种情况Django会传递一个参数给detail函数 一个正则对应一个参数
App01--->urls.py
from django.contrib import admin
from django.conf.urls import url
from app01 import views
urlpatterns = [
url(r'^login/', views.login),
]
App02-->urls.py
from django.contrib import admin
from django.conf.urls import url
from app02 import views
urlpatterns = [
url(r'^login/', views.login),
]
App01-->views.py
from django.shortcuts import render,HttpResponse,redirect
# Create your views here.
# USER_DICT={
# "k1": "root1",
# 'k2': "root2",
# 'k3': "root3",
# 'k4': "root4",
# 'k5': "root5",
# 'k6': "root6",
# }
USER_DICT={
'1': {'name':'root1', 'email':'root1@live.com'},
'2': {'name':'root2', 'email':'root2@live.com'},
'3': {'name': 'root3', 'email': 'root3@live.com'},
'4': {'name': 'root4', 'email': 'root4@live.com'},
'5': {'name': 'root5', 'email': 'root5@live.com'},
'6': {'name': 'root6', 'email': 'root6@live.com'},
}
# USER_LIST=[
# {'name':'root1'},
# {'name':'root2'},
# {'name':'root3'},
# {'name':'root4'},
# ]
# {% for item in user_list%}
def index(request,nid,uid):
#indexx
print(request.path_info)
from django.urls import reverse
# v=reverse('indexx', args=(60,50,))
v=reverse('indexx',kwargs={'nid':1,'uid': '30'})
print(v)
return render(request,'index.html',{'user_dict':USER_DICT})
# def detail(request):
# nid=request.GET.get('nid')
# detail_info=USER_DICT[nid]
# return render(request,'detail.html',{'detail_info':detail_info})
def detail(request,nid):
# return HttpResponse(nid)
# detail_info=USER_DICT[kwargs['nid']]
detail_info=USER_DICT[nid]
return render(request,'detail.html',{'detail_info':detail_info})
def login(request):
if request.method=="GET":
return render(request,'login.html')
elif request.method=="POST":
# v=request.POST.get('radio')
# print(v)
# v=request.POST.getlist('city')
# print(v)
# v=request.POST.get('fafafa')
# print(v)
obj=request.FILES.get('fafafa')
print('obj',obj.name)
import os
file_path=os.path.join('upload',obj.name)
f=open(file_path, mode="wb")
for i in obj.chunks():
f.write(i)
f.close()
from django.core.files.uploadedfile import InMemoryUploadedFile
return render(request,'login.html')
else:
#put ,DELETE,HEAD,OPTION
return redirect('/index/')
# def login(request):
# if request.method=="GET":
# return render(request,'login.html')
#
# elif request.method=="POST":
# u=request.POST.get('user')
# p=request.POST.get('pwd')
# if u=='alex' and p=='123':
# return redirect('/index/')
# else:return render(request,'login.html')
#
# else:
# #put ,DELETE,HEAD,OPTION
# return redirect('/index/')
# 查看模块源代码:ctrl+B
from django.views import View
class Home(View):
def dispatch(self,request,*args,**kwargs):
#调用父类中的dispatch
print('before') #可自定一些操作
result= super(Home,self).dispatch(request,*args,**kwargs)
print('ater') #可自定一些操作
return result
# get请求执行get post请求执行post方法
def get(self,request):
print(request.method)
return render(request,'home.html')
def post(self,request):
print(request.method)
return render(request,'home.html')
App02--->views.py
from django.shortcuts import render,HttpResponse
# Create your views here.
def login(request):
return HttpResponse('app02,login')
Templates--->index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--
<ul>
{% for k in user_dict.keys%}
<li>{{k}}</li>
{% endfor %}
</ul>
<ul>
{% for row in user_dict.values %}
<li>{{ row }}</li>
{% endfor %}
</ul>
<ul>
{% for k,row in user_dict.items %}
<li><a target="_blank" href="/detail/?nid={{k}}">{{ row.name }}</a></li>
{% endfor %}
</ul>
-->
<form action="{% url 'indexx' nid=1 uid=3 %}" method="post">
{# <form action="{{ request.path_info }}" method="post">#}
<p> <input type="text" name="user" placeholder="用户名"></p>
<p><input type="text" name="email" placeholder="邮箱"></p>
<input type="submit" value="提交">
</form>
<ul>
{% for k,row in user_dict.items %}
<li><a target="_blank" href="/detail-{{ k }}.html">{{ row.name }}</a></li>
{% endfor %}
</ul>
</body>
</html>
{# /index/ 指127.0.0.1/index/ #}
{#默认循环keys#}
{#{% for key,row in user_dict.items %} 循环key value#}
{#{% for key,row in user_dict.keys %} 循环key#}
{#{% for key,row in user_dict.values %} 循环value#}
Templates-->login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/cmdb/login/" method="post" enctype="multipart/form-data">
<p>
<input type="text" name="user" placeholder="用户名">
</p>
<p>
<input type="password" name="pwd" placeholder="密码">
</p>
<p>
男:<input type="radio" name="gender" value="1">
女:<input type="radio" name="gender" value="2">
</p>
<p>
男:<input type="checkbox" name="favor" value="11">
女:<input type="checkbox" name="favor" value="22">
</p>
<p>
<select name="city" multiple>
<option value="sh">上海</option>
<option value="bj">北京</option>
<option value="tj">天津</option>
</select>
</p>
<p>
<input type="file" name="fafafa">
</p>
<p>
<input type="submit" value="提交">
</p>
</form>
</body>
</html>
Templates-->detail.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>详细信息</h1>
<h6>用户名:{{ detail_info.name }}</h6>
<h6>邮箱:{{ detail_info.email }}</h6>
</body>
</html>
Templates-->home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/home/" method="post">
<input type="text" name="user"/>
<input type="submit"/>
</form>
</body>
</html>