一、新建一个Django程序(window 7进入cmd里面操作):注意,此处要需在指定的文件夹下
a,django-admin startproject django_test(django_test指的是项目名称)
b,进入django_test [cd django_test]
c,创建app[python manage.py startapp test_01]
d,进入project运行项目[python manage.py runserver]-------然后可以在浏览器中查看http://127.0.0.1:8000/,有It's worked,即表示创建成功
二、url常用匹配方法1--直接显示urls:
在django_test下的urls.py文件中输入以下代码
from django.conf.urls import include, url from django.contrib import admin from test_01 import views urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^articles/$', views.special_case), ]
然后在test_01的views文件中创建函数special_case,即:
from django.shortcuts import render from django.shortcuts import HttpResponse def special_case(request): return HttpResponse("It's ok")
运行http://127.0.0.1:8000/articles/即可查看【It’s ok】已经生效。
同时,还有以下几种url直接匹配方式:两种匹配方式有些许区别(具体区别后续附上)
urlpatterns = [ url(r'^articles/2003/$', views.special_case_2003), url(r'^articles/([0-9]{4})/$', views.year_archive), url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive), url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail), ]
urlpatterns = [ url(r'^articles/2003/$', views.special_case_2003), url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive), url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive), url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.article_detail), ]
此处的views文件代码如下:
def year_archive(request,year): print("---->",year) return HttpResponse(year) def month_archive(request,year,month): print("--->",year,month) return HttpResponse("%s %s"%(year,month))
三、url常用匹配方法2--通过app聚合url来使一部分urls聚集在某一个app下:
1.首先在django_test下的urls.py文件中引入payment_urls
from django.conf.urls import include, url from django.contrib import admin from test_01 import views from test_01 import urls as payment_urls urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^articles/$', views.special_case), url(r'^payment/',include(payment_urls)), ]
然后在test_01下新建一个urls.py文件,如下:
from django.conf.urls import include, url from django.contrib import admin from test_01 import views urlpatterns = [ url(r'^$',views.index), ]
最后在test_01下的views文件中创建一个index函数
def index(request): HttpResponse("*****Welcome to the payment mall*****")
需要复杂一点的话,就是此段
def index(request): if request.method == "GET": user_infos = [ {'username':'Lemon0', 'name':'Lemon Li0'}, {'username':'Lemon1', 'name':'Lemon Li1'}, {'username':'Lemon2', 'name':'Lemon Li2'}, {'username':'Lemon3', 'name':'Lemon Li3'}, ] return render(request,'test_01/index.html') else: return HttpResponse("*****Welcome to the payment mall*****")
此处需要配置django_test下的settings文件:首先在INSTALLED_APPS中加入“test_01”,然后在 TEMPLATES下改DIRS文件 'DIRS': [os.path.join(BASE_DIR,"templates")],
同时,此处引入了html文件,需要在templates下新建一个index.html文件,如果没有templates创建该文件即可,index.html文件如下。然后运行http://127.0.0.1:8000/payment/即可
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>Welcome to the payment center</h1> <ul> {% for user_obj in user_objs %} <li style="background-color:red">username:{{user_obj.username}},name:{{user_obj.name}}</li> {% endfor %} </ul> </body> </html>