Django request.path无法在包含标记模板中工作

时间:2017-07-08 05:39:10

标签: python django templates

我尝试在包含标记模板中使用request.path,但是url没有显示。 request.path在父模板中使用时可以正常工作,并且包含标记在所有其他位置正常工作。 我启用了'take_context'在包含标记中,但我不知道是否应该在views.py中指定任何特定于路径的上下文。 目前我使用render()方法从views.py:

输出逻辑
def DJ_LastDay(request):
    p = Post.objects.latest('Day')
    posts = Post.objects.filter(Day=p.Day)
    return render(request, 'blog/DJ_LastDay.html', {'DJ_LastDay_posts': posts}) 

我的收录标记的摘录:

from django import template
register = template.Library()
@register.inclusion_tag('blog/index_table.html', takes_context=True)
def DJ_LastDay(context):
    return {'posts': context['DJ_LastDay_posts']}

我的包含标签模板的片段(DJLD,DJLW,DJLM,DJLQ和DJLY都是我在父模板中启用的网址快捷方式,它们在包含模板之外正常工作):

    {% if request.path == DJLD %}
        Last Day
    {% elif request.path == DJLW %}
        Last Week
    {% elif request.path == DJLM %}
        Last Month
    {% elif request.path == DJLQ %}
        Last Quarter
    {% elif request.path == DJLY %}
        Last Year
    {% endif %}

我只需要检测条件检查的当前路径,以在我的模板中显示正确的字符串。 任何帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

您必须将请求传递给include_tag模板,就像您发布的那样'帖子'作为回报

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;


int main()
{
  struct pizza
  {
    char name[15];
    float diameter;
    int weight;
  };
  pizza *user= new pizza{};

  cout << "Hello please enter some information about your pizza" << endl;
  cout << endl << "Diameter: ";

  cin >> user->diameter;
  cout << "company name: ";
  cin.get(user->name,15);
  cout << endl << "Weight: ";
  cin >> user->weight;

  cout << "here is the information that we have assembled about your pizza company" << endl;
  cout << "Company name: "<< user->name <<e ndl;
  cout << "diameter: " << user->diameter << endl;
  cout << "weight: " << user->weight << endl;
  return 0;
}