Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将指定的kwargs设置为循环外部的url?_Python_Django - Fatal编程技术网

Python 如何将指定的kwargs设置为循环外部的url?

Python 如何将指定的kwargs设置为循环外部的url?,python,django,Python,Django,我在模板中有一些链接 <ul> {% for cat in cats %} <li><a href="{% url 'c_index' cat.slug %}">{{ cat.name }}</a> {% endear %} </ul> <ul> {% for type in types %} <li><a href="{% url 'ct_index

我在模板中有一些链接

<ul>
    {% for cat in cats %}
        <li><a href="{% url 'c_index' cat.slug %}">{{ cat.name }}</a>
    {% endear %}
</ul>
<ul>
    {% for type in types %}
        <li><a href="{% url 'ct_index' cat.slug type.slug %}">{{ type.name }}</a>
    {% endear %}
</ul>
    {猫中猫的百分比%}
  • {%endar%}
    {类型中的类型为%}
  • {%endar%}
当然,我不能访问第二个链接,因为我不能在{cats%}循环之外使用'cat.slug'

但是我想将“cat.slug”设置为第二个链接,而不使用{%forcat-in-cats%}循环

我该怎么做?例如,使用模板标记

url(r'^c_(?P<cat>[-\w]+)/$', views.c_index, name='c_index'),
url(r'^c_(?P<cat>[-\w]+)/t_(?P<type>[-\w]+)/$', views.ct_index, name='ct_index'),

def c_index(request, cat):
    c = {}

    posts = Post.objects.filter(category__slug=cat)
    cats = Category.objects.all()

    c.update({
        'posts': posts,
        'cats': cats,
    })

    return render(request, 'classifieds/index.html', c)

def ct_index(request, cat, type):
    c = {}

    posts = Post.objects.filter(category__slug=cat).filter(type=type)
    cats = Category.objects.all()
    types = Types.objects.all()

    c.update({
        'posts': posts,
        'cats': cats,
        'types': types,
    })

    return render(request, 'classifieds/index.html', c)
url(r'^c(?P[-\w]+)/$,views.c_index,name='c_index'),
url(r'^c(P[-\w]+)/t(P[-\w]+)/$”,views.ct_index,name='ct_index'),
def c_索引(请求,类别):
c={}
posts=Post.objects.filter(category\uu slug=cat)
cats=Category.objects.all()
c、 更新({
"岗位":岗位,,
“猫”:猫,
})
返回呈现(请求'classifieds/index.html',c)
def ct_索引(请求、类别、类型):
c={}
posts=Post.objects.filter(category\uu slug=cat).filter(type=type)
cats=Category.objects.all()
types=types.objects.all()
c、 更新({
"岗位":岗位,,
“猫”:猫,
“类型”:类型,
})
返回呈现(请求'classifieds/index.html',c)

在视图中,您需要添加
category=category.objects.get(slug=cat)
。然后将
category
变量添加到上下文中。
然后在模板中,可以使用在上下文中定义的模板变量访问该类别。如果是
category
,您的url标记将如下所示:
{%url'ct_index'category.slug type.slug%}

您希望第二个链接显示在您的猫列表中的每只猫上,还是只显示一只猫?如果只有一只猫,你怎么知道是哪只?只有一只猫。我想在当前url内的第二个链接使用一个。url(r“^c”(-P[-\w]+)/$”,views.c_index,name='c_index')你能发布你的视图代码吗?我添加了视图代码。
类别和
类型
模型是否有某种关联(通过
外键
或其他方式)?