Django:如何在模板中包含视图 [英] Django: How to include a view from within a template

查看:95
本文介绍了Django:如何在模板中包含视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是django的新人,我想知道什么是最好的/推荐的方法来包含模板中的视图(具有某些逻辑及其生成的HTML)。

I'm new to django and I was wondering what is the best/recommend approach to include a view (with certain logic, and its resulting HTML) from within a template.

我的具体例子如下:
我有一个模型是:

My concrete example is as follows: I have a model which is:

class City(models.Model):    
    name = models.CharField(max_length=200)        

class Place(models.Model):    
    city = models.ForeignKey(City)
    name = models.CharField(max_length=200)
    state = models.IntegerField()

所以我需要一个展示每个城市的地方。每个地方都应以不同的方式呈现,具体取决于其状态。每个不同的方式是非常不同的,可能需要相当的一些商业逻辑。

So I need a view to show each city and it's places. Each place should be rendered in a different way depending on its state. Each different way is very different and will probably need quite some bussiness logic.

根据我对其他框架的经验,我想象如下。从城市模板中调用一个视图来渲染位置,这将具有所有必要的逻辑,并将选择使用哪个模板来呈现地点:$ b​​ $ b示例伪代码:

From my experience with other frameworks I imagined something as follows. From withing the cities template call a view that will render the locations, which will have all the necessary logic and would choose which template to use to render the place: Example pseudo-code:

cities.html

cities.html

{% for place in places %}    
   {% include_view 'show_place' place %}    
{% endfor %}

include_view标签在哪里调用视图,它将决定给定地点使用哪个模板:

Where the include_view tag would call a view, which would decide which template to use for the given place:

def show_place(request, place):    
    # Here I will probably use more logic to prepare the template.
    template_name = "places/default.html"
    if place.state == 1 :
        template_name = 'places/active.html'
    if place.state == 2 :
        template_name = 'places/started.html'
    if place.state == 3 :
        template_name = 'places/completed.html'
    # etc

    return render(request, template_name, { 'place': place } )

是这个方法可能在django?

我已经看到使用这样的包含标签的可能性:在模板中添加视图
Template include和django views / urls。他们的工作方式如何(do / should)?

I'v seen the possibility of using an include tag like here: Include a view in a template or Template includes and django views/urls. How (do/should) they work?

但是这样做会强制用一个固定的模板注册自定义模板函数:
register.inclusion_tag 'results.html')(show_results)#在这里我需要逻辑,并使'results.html'动态。

but this forces to register the custom template function with a fixed template: register.inclusion_tag('results.html')(show_results) #here I would need logic, and make 'results.html' dynamic.

另外我想把那个show_place作为一个正常视图可以直接访问。

Also I would like to have that show_place as a normal view to be able to access it directly.

我还可以在IF模板中查询状态,并使用{%include%}标签,但是将允许我有另外的商务逻辑,在另一种方法的视图中:

I can also do the IFs asking for the state inside the template and use the {% include %} tag, but that would allow me to have additional bussiness logic that would be in the other approach in the view:

{% for place in places %}    
    {% if place.state = 1 %}    
        {% include 'places/active.html' %}    
    {% if ... }    
                {% include ... }    
{% endfor %}

完成此项工作的最佳方法是什么?

What is the best way of accomplishing this?

谢谢!

推荐答案

需要使用的是 Django自定义模板标签。你可以简单地在你的模板中传递一个变量,然后让它决定在当前模板中插入哪个视图和模板。

What you are going to need to use is a Django Custom Template Tag. You could simply pass a variable to it in your template, then have it decide which view and template to insert in your current template.

一个例子:

您的自定义模板标签(rend_item.py)

Your Custom Template Tag (rend_item.py)

def return_template(item):
    template = 'null'

    context = {
            #Dictionary of things to pass back

    }
    #if statements that choose what 'template' should be
    if item == 5:
        template = 'item5.html'
    else:
        template = 'default.html'

    #render the template
    return render_to_string(template, context)

模板

 {# Make sure you load the py file where your template tag is located at the top #}
 {% load rend_item %}

      {% for item in cart %}

         {{ item|return_template }}         

      {% endfor %}

这篇关于Django:如何在模板中包含视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆