django中将views.py中的python方法传递给html模板文件
Posted 酷酷的狐狸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django中将views.py中的python方法传递给html模板文件相关的知识,希望对你有一定的参考价值。
常规的模板渲染
from django.db import models # Create your models here. class ArticalType(models.Model): caption = models.CharField(max_length=16) class Category(models.Model): caption = models.CharField(max_length=16) class Artical(models.Model): title = models.CharField(max_length=32) content = models.CharField(max_length=255) category = models.ForeignKey(Category, on_delete=models.CASCADE) artical_type = models.ForeignKey(ArticalType, on_delete=models.CASCADE)
urlpatterns = [ path(‘admin/‘, admin.site.urls), re_path(‘artical-(?P<artical_type_id>d+)-(?P<category_id>d+).html‘, backend.artical), ]
from django.shortcuts import render from jax import models def artical(request, *args, **kwargs): print(kwargs) artical_type = models.ArticalType.objects.all() category = models.Category.objects.all() for k,v in kwargs.items(): if v == ‘0‘: artical = models.Artical.objects.all() else: artical = models.Artical.objects.filter(**kwargs) return render(request, "artical.html", {"artical_type": artical_type, "category": category, "artical": artical,})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .tag a{ display: inline-block; padding: 3px 5px; border: 1px solid #dddddd; margin: 5px 5px; {#text-decoration: none;#} } .tag a.w{ background: #2459a2; } </style> </head> <body> <h1>搜索条件</h1> <div class="tag"> <div> {% if artical_list.artical_type_id == 0 %} <a class="w" href="/artical-0-{{ artical_list.category_id }}.html">全部</a> {% else %} <a href="/artical-0-{{ artical_list.category_id }}.html">全部</a> {% endif %} {% for row in artical_type %} {% if row.id == artical_list.artical_type_id %} <a class="w" href="/artical-{{ row.id }}-{{ artical_list.category_id }}.html">{{ row.caption }}</a> {% else %} <a href="/artical-{{ row.id }}-{{ artical_list.category_id }}.html">{{ row.caption }}</a> {% endif %} {% endfor %} </div> <div> {% if artical_list.category_id == 0 %} <a class="w" href="/artical-{{ artical_list.artical_type_id }}-0.html">全部</a> {% else %} <a href="/artical-{{ artical_list.artical_type_id }}-0.html">全部</a> {% endif %} {% for row in category %} {% if row.id == artical_list.category_id %} <a class="w" href="/artical-{{ artical_list.artical_type_id }}-{{ row.id }}}}.html">{{ row.caption }}</a> {% else %} <a href="/artical-{{ artical_list.artical_type_id }}-{{ row.id }}.html">{{ row.caption }}</a> {% endif %} {% endfor %} </div> </div> <h1>搜索结果</h1> <ul> {% for row in artical %} <li>{{ row.id }} - {{ row.title }}</li> {% endfor %} </ul> </body> </html>
将views.py中的python方法传递给html模板文件:
from django.shortcuts import render from jax import models def artical(request, *args, **kwargs): print(kwargs) artical_type = models.ArticalType.objects.all() category = models.Category.objects.all() for k,v in kwargs.items(): kwargs[k] = int(v) # 新添加 if v == ‘0‘: artical = models.Artical.objects.all() else: artical = models.Artical.objects.filter(**kwargs) return render(request, "artical.html", {"artical_type": artical_type, "category": category, "artical": artical, ‘artical_list‘: kwargs}) # 新添加
在app下新建目录templatetags(必须为这个名字),里面可以自定义文件名称,这里定义为filter.py
from django import template from django.utils.safestring import mark_safe register = template.Library() @register.simple_tag def filter_all(artical_list, k): ‘‘‘ {% if artical_list.artical_type_id == 0 %} <a class="w" href="/artical-0-{{ artical_list.category_id }}.html">全部</a> {% else %} <a href="/artical-0-{{ artical_list.category_id }}.html">全部</a> {% endif %} :return: ‘‘‘ if k == "artical_type_id": n1 = artical_list["artical_type_id"] n2 = artical_list["category_id"] if n1 == 0: ret = ‘<a class="w" href="/artical-0-%s.html">全部</a>‘ % n1 else: ret = ‘<a href="/artical-0-%s.html">全部</a>‘ % n2 else: n1 = artical_list["category_id"] n2 = artical_list["artical_type_id"] if n1 == 0: ret = ‘<a class="w" href="/artical-%s-0.html">全部</a>‘ % n2 else: ret = ‘<a href="/artical-%s-0.html">全部</a>‘ % n2 return mark_safe(ret) @register.simple_tag def filter_artical_type(artical_type, artical_list): """ {% for row in artical_type %} {% if row.id == artical_list.artical_type_id %} <a class="w" href="/artical-{{ row.id }}-{{ artical_list.category_id }}.html">{{ row.caption }}</a> {% else %} <a href="/artical-{{ row.id }}-{{ artical_list.category_id }}.html">{{ row.caption }}</a> {% endif %} {% endfor %} :return: """ ret = [] for row in artical_type: if row.id == artical_list["artical_type_id"]: temp = ‘<a class="w" href="/artical-%s-%s.html">%s</a>‘ % (row.id, artical_list["category_id"],row.caption) else: temp = ‘<a href="/artical-%s-%s.html">%s</a>‘ % (row.id, artical_list["category_id"], row.caption) ret.append(temp) return mark_safe(‘‘.join(ret)) @register.simple_tag def filter_category(category, artical_list): """ {% for row in category %} {% if row.id == artical_list.category_id %} <a class="w" href="/artical-{{ artical_list.artical_type_id }}-{{ row.id }}}}.html">{{ row.caption }}</a> {% else %} <a href="/artical-{{ artical_list.artical_type_id }}-{{ row.id }}.html">{{ row.caption }}</a> {% endif %} {% endfor %} :param category: :param artical_list: :return: """ ret = [] for row in category: if row.id == artical_list["category_id"]: temp = ‘<a class="w" href="/artical-%s-%s.html">%s</a>‘ % (artical_list["artical_type_id"], row.id, row.caption) else: temp = ‘<a href="/artical-%s-%s.html">%s</a>‘ % (artical_list["artical_type_id"], row.id, row.caption) ret.append(temp) return mark_safe(‘‘.join(ret))
前端html文件倒入方法,以及传递参数
{% load filter %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .tag a{ display: inline-block; padding: 3px 5px; border: 1px solid #dddddd; margin: 5px 5px; {#text-decoration: none;#} } .tag a.w{ background: #2459a2; } </style> </head> <body> <h1>搜索条件</h1> <div class="tag"> <div> {% filter_all artical_list ‘artical_type_id‘ %} {# {% if artical_list.artical_type_id == 0 %}#} {# <a class="w" href="/artical-0-{{ artical_list.category_id }}.html">全部</a>#} {# {% else %}#} {# <a href="/artical-0-{{ artical_list.category_id }}.html">全部</a>#} {# {% endif %}#} {% filter_artical_type artical_type artical_list %} {# {% for row in artical_type %}#} {# {% if row.id == artical_list.artical_type_id %}#} {# <a class="w" href="/artical-{{ row.id }}-{{ artical_list.category_id }}.html">{{ row.caption }}</a>#} {# {% else %}#} {# <a href="/artical-{{ row.id }}-{{ artical_list.category_id }}.html">{{ row.caption }}</a>#} {# {% endif %}#} {# {% endfor %}#} </div> <div> {% filter_all artical_list ‘category_id‘ %} {# {% if artical_list.category_id == 0 %}#} {# <a class="w" href="/artical-{{ artical_list.artical_type_id }}-0.html">全部</a>#} {# {% else %}#} {# <a href="/artical-{{ artical_list.artical_type_id }}-0.html">全部</a>#} {# {% endif %}#} {% filter_category category artical_list %} {# {% for row in category %}#} {# {% if row.id == artical_list.category_id %}#} {# <a class="w" href="/artical-{{ artical_list.artical_type_id }}-{{ row.id }}}}.html">{{ row.caption }}</a>#} {# {% else %}#} {# <a href="/artical-{{ artical_list.artical_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>#} {# {% endif %}#} {# {% endfor %}#} </div> </div> <h1>搜索结果</h1> <ul> {% for row in artical %} <li>{{ row.id }} - {{ row.title }}</li> {% endfor %} </ul> </body> </html>
转换时需要注意:
1.在app下新建的目录名字必须为templatetags。
2.html文件第一行需要添加"{% load 文件名 %}",以此来倒入自定义前端方法中的函数。
3.新建的函数文件中需要倒入模块
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
以及需要对自定义方法添加装饰器
@register.simple_tag
def fun(*args, **kwargs):
pass
以上是关于django中将views.py中的python方法传递给html模板文件的主要内容,如果未能解决你的问题,请参考以下文章
一 .Django 视图Django views.py中的 FBV(函数) CBV(类)
使用JavaScript Onclick Event将数据传递给Django中的views.py?