标签:*args userinfo 分享 不用 books 分页 obj 接口 das
django之url参考文档:https://yiyibooks.cn/xx/Django_1.11.6/topics/http/urls.html
在setting中ROOT_URLCONF = ‘devops.urls‘ 配置
Django如何处理请求
匹配url,都是从第一个往下匹配,若第一个可以匹配,就不用跳到第二个url规则
from django.conf.urls import url,include
from . import views
app_name = ‘dashboard‘
urlpatterns = [
url(r‘^$‘, views.index,name=‘index‘),
url(r‘^login/$‘, views.loginView,name=‘login‘),
url(r‘^login/‘, views.loginView1,name=‘login1‘),
]
每个正则表达式前面的‘r‘ 是可选的但是建议加上。 它告诉Python 这个字符串是“原始的” ,表示:字符串中任何字符都不应该转义。
url映射一般是一条正则表达式,“^” 字符串的开始,“$“ 字符串的结束
例子1:访问“http://127.0.0.1:8000/dashboard/login/123” 匹配的是 url(r‘^login/‘, views.loginView1,name=‘login1‘) 这条
: 访问“http://127.0.0.1:8000/dashboard/login” 匹配的是 url(r‘^login/$‘, views.loginView,name=‘login‘) 这条
例子2:
from django.conf.urls import url
from . import views
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),
]
匹配第一个:
访问:“http://127.0.0.1:8000/dashboard/articles/2003/”
匹配第二个:
访问:“http://127.0.0.1:8000/dashboard/articles/2013/”
匹配第三个:
访问:“http://127.0.0.1:8000/dashboard/articles/2013/12/”
匹配第四个:
访问:“http://127.0.0.1:8000/dashboard/articles/2014/12/12312123123213/”
def article_detail(request,*args,**kwargs):
print("args:",args)
print("kwargs:",kwargs)
return HttpResponse(‘12‘)
结果:
args: (‘2014‘, ‘12‘, ‘12312123123213‘)
kwargs: {}
关键字参数
from django.conf.urls import url
from . import views
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),
]
url(r‘^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$‘, views.article_detail),
def article_detail(request,*args,**kwargs):
return HttpResponse(json.dumps(kwargs))
结果:
include访问:
urlpatterns = [
url(r"^user/", include([
url(r‘^list/$‘, view.userlist, name="user_list"),
url(r‘^info/$‘, view.userinfo, name="userer_inf),
url(r‘^modify/‘, include([
url(r‘status/$‘,view.modifystatus, name="user_modify_status"),
]))
]))
]
访问:‘http://127.0.0.1:8000/user/list/’
‘http://127.0.0.1:8000/user/info/’
‘http://127.0.0.1:8000/user/modify/’
‘http://127.0.0.1:8000/user/modify/status/’
以函数的形式进行定义的视图就是函数视图
以类的的形式进行定义的视图就是类视图
例子:展示页面的增删改查功能
用函数视图实现:
结果:
用类视图实现:
.as_view() 为类视图提供一个函数入口
url(r‘^login/$‘, views.LoginUser.as_view(),name=‘login‘),
from django.views import View
分页练习:
1、from django.core.paginator import Paginator
2、
class UserViewV4(View):
def get(self,request,*args,**kwargs):
queryset = User.objects.all()
paginator = Paginator(queryset,20) #20条数据为一页,实例化分页对象
try:
page = int(request.GET.get(‘page‘))
except:
page = 1
if page < 1:
page = 1
page = paginator.page(page) #取对象的第几分页对象;page.object_list:为第几分页的内容
data = [{‘id‘:user.id,‘email‘:user.email,‘username‘:user.username} for user in page.object_list]
return JsonResponse(data,safe=False)
例子2:
通过给定的接口创建普通用户密码
class UserViewV5(View):
def post(self,request,*args,**kwargs):
data = request.POST.dict()#接收所有的dict
print(data)
try:
user = User.objects.create_user(**data)
except IntegrityError:
return JsonResponse({‘error‘:‘用户已存在‘})
return JsonResponse({‘id‘:user.id,‘email‘:user.email,‘username‘:user.username})
create_user接收的参数,data那里必须要对应起来,不然添加不了,:
标签:*args userinfo 分享 不用 books 分页 obj 接口 das
原文地址:http://blog.51cto.com/jacksoner/2307465