第一种
from django.http import HttpResponse,JsonResponse
def hello(request):
dic={'a':'1'}
return JsonResponse(dic)


urls.py
from django.contrib import admin
from django.urls import path,re_path
from .views import hello

urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^hello/$',hello)
]

Django  小笔记_django

Django  小笔记_django_02

Django  小笔记_django_03

第二种

views.py

from  django.http import  HttpResponse,JsonResponse
def hello(request,s):
dic = {
's':s
}
return JsonResponse(dic)


urls.py
from django.contrib import admin
from django.urls import path,re_path
from .views import hello

urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^hello/(.+)/$',hello)
]


第三种:
views.py
from  django.http import  HttpResponse,JsonResponse
def hello(request,*args):
a=args[0]
b=args[1]
dic = {
'a': a,
'b': b
}
return JsonResponse(dic)


url.py
from django.contrib import admin
from django.urls import path,re_path
from .views import hello

urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^hello/(.*)/(.*)/$',hello)
]
#########################################################
Httpresponse 是返回对象 数组 变量都可以 如果是想返回字典 需要import json  
json.dumps(dic)

Jsonresponse 是返回字典

##############################################################################################################################################################################################################################
from  django.http import  HttpResponse,JsonResponse
urlpatterns = [
path('admin/', admin.site.urls),
#re_path(r'^hello/(.*)/(.*)/$',hello)
re_path(r'^hello/(?P<pk>\d+)/$',hello)
]

def hello(request,**kwargs):
pk = kwargs.get('pk')
print(pk)
dic = {
'pk is ':'{}'.format(pk)
}
return JsonResponse(dic)


Django  小笔记_django_04

 

 

def hello(request,**kwargs):
pk = kwargs.get('pk')
print(pk)
context = {
'pk':pk
}
print(context)
return render(request,'1.html',context)


urlpatterns = [
path('admin/', admin.site.urls),
#re_path(r'^hello/(.*)/(.*)/$',hello)
re_path(r'^hello/(?P<pk>\d+)/$',hello)
]


Django  小笔记_django_05

Django  小笔记_django_06

Django  小笔记_django_07

 

 

def hello2(request,*args):
a=args[0]
print(a)
context = {
'a': a,
}

return render(request,'1.html',context)


urlpatterns = [
path('admin/', admin.site.urls),
#re_path(r'^hello/(?P<pk>\d+)/$',hello)
re_path(r'^hello2/(.*)/$',hello2),

]

Django  小笔记_django_08

 

class hello(View):
def get(self,request,*args,**kwargs):
context = {
'name': 'lili',
'age': '18',
'type': args[0]
}
return render(request,'1.html',context)
def post(self,request,*args,**kwargs):
data = QueryDict(request.body).dict()
print(data)
return JsonResponse({})
def put(self,request,*args,**kwargs):
data = QueryDict(request.body).dict()
print(data)
return JsonResponse({data})


data = request.GET.get('page')
abc.com/cmdb/idc_page?page=2&search=


$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
from  .models import  *
from django.forms.models import model_to_dict

def helloe(request,**kwargs):
di = model_to_dict(Group.objects.get(id=1))
print(di)
context={'1':'2'}
return render(request,'1.html',context)

把对象转为dict返回给前端


{'id': 1, 'name': 'ttt'}

Django  小笔记_django_09

 

 

#############################################

from   cmdb.models import   *
idc = Idc.objects.get(id=1)
idc
<QuerySet [<Idc: Idc object (1)>]>
idc.values()
<QuerySet [{'id': 1, 'name': 'IDC1', 'remark': 'IDC1', 'address': '苏州', 'price': '0'}]>

 

Django  小笔记_django_10

Django  小笔记_django_11