day98
#根据名称反向生成url from django.conf.urls import url,include from django.contrib import admin from django.shortcuts import HttpResponse,redirect from app01 import urls from niubin.service import v1 def index(request): from django.urls import reverse #根据名称反向生成url # test_url = reverse('xx') #反向生成url没参数 # test_url = reverse('xx',args=(2,9)) #反向生成url有参数 test_url = reverse('xx',kwargs={'a2':2,'a1':9}) #反向生成url命名参数 print(test_url) return HttpResponse('index') def test(request): return HttpResponse('test') urlpatterns = [ url(r'^index/', index), url(r'^test/asdf/asdf/asf/asdf/asf', test,name='xx'), url(r'^test/asdf/(\d+)/asf/(\d+)/asf', test,name='xx'), url(r'^test/asdf/(?P<a1>\d+)/asf/(?P<a2>\d+)/asf', test,name='xx'), url(r'nb/', v1.site.urls),]
{% extends 'admin/base_site.html' %}
{% load il8n admin_urls static admin_list %}
{% block extrastyle %}
{% url 'xx' %} #前端使用反向生成url
{% url 'xx' 2 9 %} #有参数
{% url 'xx' a1=2 a2=9 %} #有名字
{% endblock %}
#namespace有值时必须传入 def index(request): from django.urls import reverse #根据名称反向生成url # test_url = reverse('xx') #反向生成url没参数 # test_url = reverse('xx',args=(2,9)) #反向生成url有参数 # test_url = reverse('xx',kwargs={'a2':2,'a1':9}) #反向生成url命名参数 # print(test_url) url_test = reverse('x1:tst') #必须传入namespace print(url_test) return HttpResponse('ok') def test(request): return HttpResponse('test') urlpatterns = [ # url(r'^index/', index), # # url(r'^test/asdf/asdf/asf/asdf/asf', test,name='xx'), # # url(r'^test/asdf/(\d+)/asf/(\d+)/asf', test,name='xx'), # url(r'^test/asdf/(?P<a1>\d+)/asf/(?P<a2>\d+)/asf', test,name='xx'), url(r'^app01/',([ url(r'^index/', index,name='idx'), url(r'^test/', test,name='tst'), ],'x1','x1')), #如果url出现了namespace(有值),必须传入]
#多层namespace,从外到内,每个都要传 from django.conf.urls import url,include from django.contrib import admin from django.shortcuts import HttpResponse,redirect from app01 import urls from niubin.service import v1 from django.urls import reverse def index(request): from django.urls import reverse #根据名称反向生成url # test_url = reverse('xx') #反向生成url没参数 # test_url = reverse('xx',args=(2,9)) #反向生成url有参数 # test_url = reverse('xx',kwargs={'a2':2,'a1':9}) #反向生成url命名参数 # print(test_url) url_test = reverse('x1:x2:tst') #如果有多个从外到内,每个都要传 print(url_test) return HttpResponse('ok') def test(request): return HttpResponse('test') urlpatterns = [ # url(r'^index/', index), # # url(r'^test/asdf/asdf/asf/asdf/asf', test,name='xx'), # # url(r'^test/asdf/(\d+)/asf/(\d+)/asf', test,name='xx'), # url(r'^test/asdf/(?P<a1>\d+)/asf/(?P<a2>\d+)/asf', test,name='xx'), url(r'^app01/',([ url(r'^index/', ([ url(r'^index/', index, name='idx'), url(r'^test/', test, name='tst'), ],'x2','x2')), ],'x1','x1')), #如果url出现了namespace(有值),必须传入 url(r'nb/', v1.site.urls),]
#changelist_view 生成器减少循环 def changelist_view(self,request,*args,**kwargs): data_list = self.model_class.objects.all() #获取到所有的表对象 def headers(): #把headers做成生成器,减少后端循环 for v in self.list_display: verbose_name = self.model_class._meta.get_field(v).verbose_name # 获取到当前字段的verbose_name yield verbose_name context = { 'data_list':data_list, 'list_display':self.list_display, 'headers':headers, } return render(request,'changelist.html',headers()) #传入生成器