Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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 Django 2.1调用旧的slug url变量_Python_Django_Listview_Url - Fatal编程技术网

Python Django 2.1调用旧的slug url变量

Python Django 2.1调用旧的slug url变量,python,django,listview,url,Python,Django,Listview,Url,我在通过以下类型的路径引导url模式时遇到一些问题:Listview-->Listview2-->DetailView。我的url模式遇到了麻烦。以下是我的工作内容: app_name = 'ism' urlpatterns = [ path('', views.IndexView.as_view(), name='index'), path('<slug:client_slug>/', views.CostCenterListView.as_view(), name='cost_c

我在通过以下类型的路径引导url模式时遇到一些问题:Listview-->Listview2-->DetailView。我的url模式遇到了麻烦。以下是我的工作内容:

app_name = 'ism'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<slug:client_slug>/', views.CostCenterListView.as_view(), name='cost_center_list'),
path('<slug:client_slug>/<slug:cost_center_slug>/', views.cost_center_detail, name='cost_center_detail'),
]
你能确认我的问题是我的模板中的{%url%}没有记住url路径中的第一个slug吗?我的错误消息似乎表明它正在尝试查找:

.../cost_center_slug 
而不是:

.../client_slug/cost_center_slug/
我的假设是django会神奇地记住我的url模式的第一部分(即client_slug),但这似乎没有发生。我是否需要在视图中引入更多的上下文变量,以允许在上面的模板中调用两个url变量(url变量也是正确的术语吗?听起来不正确)

这是我的完整错误消息('cffd'是代表成本中心的slug):


我想你的网址错了。应该是

{% url 'ism:cost_center_list' client_slug %}
或者(您没有提供足够的参数来构造
cost\u center\u detail
url)


你能完整地回溯整个错误信息吗?你的诊断是错误的,这表明当你看到错误时你所处的页面。但您需要显示视图和完整错误回溯。您可以添加完整回溯以了解您的问题。我假设,在你的第二个url中,你直接调用slug。我的建议是添加`path(someword//),并对第三个url执行此操作。嘿@DanielRoseman-我在问题中添加了我的观点。谢谢,你的第二个建议也是我认为的问题所在。但是,我不知道如何在我的第二个ListView(CostCenterListView)中获取client.slug作为上下文变量。我的查询集是client.costcenter\u set.all(),我需要它来生成该特定客户端的成本中心列表,但我无法使用该查询集获取对象client.slug。我是否需要在ListView中使用另一个get\u object/get\u context\u data方法?从视图的代码来看,我认为您可以执行
cost\u center.client.slug
。如果你定义了其他的东西,我可能会错,但它应该非常接近,没有看到
models.py
的效果。非常感谢你!
.../client_slug/cost_center_slug/
Reverse for 'cost_center_detail' with arguments '('cffd',)' not found. 
1 pattern(s) tried: 
['ism/(?P<client_slug>[-a-zA-Z0-9_]+)/(?P<cost_center_slug>[-a-zA-Z0-9_]+)/$']
class IndexView(generic.ListView):
    template_name = 'ism/index.html'        
    context_object_name = 'client_list'     

    def get_queryset(self):
        queryset = Client.objects.all()
        return queryset


class CostCenterListView(generic.ListView):
    template_name = 'ism/costcenter_list.html'
    context_object_name = 'cost_centers'

    def get_queryset(self):
        slug = self.kwargs.get('client_slug')
        client = Client.objects.get(slug=slug)
        queryset = client.costcenter_set.all()
        return queryset


def cost_center_detail(request, client_slug, cost_center_slug):
    cost_center = get_object_or_404(CostCenter, slug=cost_center_slug)
    context = {'cost_center': cost_center}
    return render(request, 'ism/costcenter_detail.html', context)
{% url 'ism:cost_center_list' client_slug %}
{% url 'ism:cost_center_detail' client_slug cost_center.slug %}