1.@register.filter 只能夠傳2個參數,前端能夠if判斷html
2.@register.simple_tag 自定製模板前端
3. ajax python
一、@register.simple_tag 自定製模板jquery
# 自定義simple__tag 不能夠作if判斷 ##views.py def student(request): # student # studentDetail detail_list = models.StudentDetail.objects.filter(student__status=1).values('letter_of_thanks', "student__name", "student__salary", 'student__company', 'student__pic') ##獲取數據庫的內容 print(detail_list) return render(request, 'student.html', {'detail_list': detail_list}) ##返回到前端 在app1下面建立 templatetags 文件夾 建立文件 xx.py from django import template from django.utils.safestring import mark_safe from django.template.base import resolve_variable, Node, TemplateSyntaxError register = template.Library() @register.simple_tag def detail1(item,counter,allcount,remainder): # counter 當前循環的索引 allcount總列數 #餘數 temp = """ <div style="width: 245px;"> <img style="width: 245px;height: 200px;" src="/%s"> <p>%s</p> <p>%s</p> <p>%s</p> </div> """ if counter%allcount == remainder: #當前循環的索引 除以總列數(4列),獲得餘數 temp = temp %(item['student__pic'], item['student__name'], item['student__salary'], item['letter_of_thanks']) return mark_safe(temp) # mark_safe 傳入的不是字符串,是前端代碼 else: return "" # HTMl的文件 # {% load xx %} {#傳入模塊#} <!DOCTYPE html> <body> <div> </div> <style> .clearfix:after{ content: '.'; visibility: hidden; height: 0; clear: both; display: block; } </style> <div style="margin: 0 auto;width: 980px;" class="clearfix"> <div style="width: 245px;float: left"> {% for item in detail_list %} {# 循環前端發來的數據 #} {% detail1 item forloop.counter 4 1 %} {# 執行那個模塊中的方法 餘數等於1的話放到這 #} {# forloop.counter當前循環到多少 4列 #} {% endfor %} </div> <div style="width: 245px;float: left"> {% for item in detail_list %} {% detail1 item forloop.counter 4 2 %} {# 執行那個模塊中的方法 餘數等於3的話放到這 #} {% endfor %} </div> <div style="width: 245px;float: left"> {% for item in detail_list %} {% detail1 item forloop.counter 4 3 %} {# 執行那個模塊中的方法 餘數等於4的話放到這 #} {% endfor %} </div> <div style="width: 245px;float: left"> {% for item in detail_list %} {% detail1 item forloop.counter 4 0 %} {# 執行那個模塊中的方法 餘數等於0的話放到這 #} {% endfor %} </div> </div>
#views.py def student(request): # student # studentDetail detail_list = models.StudentDetail.objects.filter(student__status=1).values('letter_of_thanks', "student__name", "student__salary", 'student__company', 'student__pic') ##獲取數據庫的內容 print(detail_list) return render(request, 'student.html', {'detail_list': detail_list}) ##返回到前端 在app1下面建立 templatetags 文件夾 建立文件 xx.py @register.filter ##只能傳2個參數 def detail3(value, arg): ##value循環的到的當前索引 這裏的arg 是總列數,和餘數 ''' :param values: :param arg: :return: ''' allcount, remainder = arg.split(',') ##把總列數和餘數進行分割 allcount = int(allcount) ##轉換類型 remainder = int(remainder) if value % allcount == remainder: ##求餘數 return True return False # html.py {% load xx %} {#傳入模塊#} <!DOCTYPE html> <body> <div style="width: 245px;float: left"> {% for item in detail_list %} {# 循環前端發過來的數據 #} {% if forloop.counter|detail3:"4,0" %} {# 循環到的索引 函和'總列數,餘數' 放到detail3這個方法中 這裏這能夠傳2個參數 ,要循環除別的列,直接改餘數就能夠 #} <div style="width: 245px;"> <img style="width: 245px;height: 200px;" src="/{{ item.student__pic }}"> {# 圖片 #} <p>{{ item.student__name }}</p> {# 姓名 #} <p>{{ item.student__salary }}</p> {# 。。。。 #} <p>{{ item.letter_of_thanks }}</p> </div> {% endif %} {% endfor %} </div>
# view.pyajax
def student1(request): if request.method == 'POST': detail_list = models.StudentDetail.objects.filter(student__status=1).values('letter_of_thanks', "student__name", "student__salary", 'student__company', 'student__pic') detail_list = list(detail_list) ##數據庫獲得的數據轉換成列表類型 return HttpResponse(json.dumps(detail_list)) ##轉換成字符串 # student # studentDetail # detail_list = models.StudentDetail.objects.filter(student__status=1).values('letter_of_thanks',"student__name","student__salary",'student__company', 'student__pic') return render(request,'student1.html') ##get發送頁面
# student1.html數據庫
{% load xx %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> </div> <style> .clearfix:after{ content: '.'; visibility: hidden; height: 0; clear: both; display: block; } .c1{ width: 245px; } .c1 img{ width: 245px; height: 200px; } </style> <div id="container" style="margin: 0 auto;width: 980px;" class="clearfix"> <div style="width: 245px;float: left"> </div> <div style="width: 245px;float: left"> </div> <div style="width: 245px;float: left"> </div> <div style="width: 245px;float: left"> </div> </div> <script src="/static/js/jquery-2.1.4.min.js"></script> <script> $(function () { $.ajax({ url: '/student1/', type: 'POST', dataType: 'json', success: function (arg) { //arg = JSON.parse(arg); // arg = JSON.stringify(arg) $.each(arg, function (k,v) { k = k + 1; var div = document.createElement('div'); //建立標籤 div.className = 'c1'; //定義屬性 var img = document.createElement('img'); // 建立img標籤 rat =img.src = "/" + v.student__pic; // 傢伙是哪一個屬性 console.log(rat); //傳過來是路勁 var p = document.createElement('p'); // cnm =p.innerText = v.letter_of_thanks; // console.log(cnm); //這裏輸出內容 div.appendChild(img); // div.appendChild(p); // if(k%4 == 1){ $('#container').children(':eq(0)').append(div); //children 全部的子元素, eq 索引 }else if(k%4 == 2){ $('#container').children(':eq(1)').append(div); }else if(k%4 == 3){ $('#container').children(':eq(2)').append(div); }else if(k%4 == 0){ $('#container').children(':eq(3)').append(div); }else{ } }) } }) }) </script> </body> </html>