当urls.py

时间:2017-09-08 14:45:37

标签: python django listview django-views django-class-based-views

我想获得Sample的Project名称。它应该在self.args中,因为我的urls.py看起来像那样(有很多网址,所以我只发布重要内容):

项目/ urls.py

app_name = 'projects'
urlpatterns = [
    url(r"^$", ProjectListView.as_view(), name="projects_list"),
    url(r"^(\w+)/robjects/", include("robjects.urls")),
    url(r"^(\w+)/tags/$", TagsListView.as_view(), name="tag_list"),
...
]

robjects / urls.py

app_name="robjects"
urlpatterns = [
...
    url(r'^(?P<pk>[0-9]+)/samples/', include("samples.urls")),
]

样本/ urls.py

app_name="samples"
urlpatterns = [
    url(r'^list/$', SampleListView.as_view(), name='sample_list_view'),
    url(r'^create/$', SampleCreateView.as_view(), name='sample_create_view'),
    url(r'^(?P<sample_id>[0-9]+)/update/$', SampleUpdateView.as_view(), name='sample_update'),
    url(r'^(?P<sample_id>[0-9]+)/delete/$', SampleDeleteView.as_view(), name='sample_delete'),
]

我想获取project_name,即projects / urls.py的'(\ w +)'部分。为此,我在samples / views.py中编写了该代码。我试着做project_name = self.args [0],但我的args是空的。

class SampleListView(ListView):         model =样本         template_name =“samples / samples_list.html”

    def get(self, request, *args, **kwargs):
        """A base view for displaying a list of objects."""
        # check if project exists
        try:
            pk = kwargs['pk']
            robject = Robject.objects.get(id=pk)
            # add project to view attributes
            self.robject = robject
        except Robject.DoesNotExist:
            raise Http404
        print(self.args) # it should be a project name here
        return super(SampleListView, self).get(request, *args, **kwargs)

    def get_queryset(self, *args, **kwargs):
        """
        Overwrite orginal qs and add filtering by robject
        """

        # original queryset
        qs = super().get_queryset(*args, **kwargs)
        print('args, ',self.args)
        print('kwargs', self.kwargs)
        pk = self.kwargs['pk']
        robject = Robject.objects.get(id=pk)
        return qs.filter(robject=self.robject)

这是输出:

args,  ()
kwargs {'pk': '27'}

我想通过robject_id和project_name过滤我的qs,对于robject_id,它可以正常工作。我不知道如何获取project_name进行过滤。

此外我不得不说,在标签中(“没有包含在网址中”)我使用self.argss [0]进行了它并且它可以工作,但只能形成标签ListView。

以下代码:

class TagsListView(LoginRequiredMixin, ListView):
    model = Tag
    template_name = "tags/tag_list.html"

    def get(self, request, *args, **kwargs):
        """A base view for displaying a list of objects."""
        # check if project exists
        try:
            project = Project.objects.get(name=args[0]) # it works fine
            # add project to view attributes
            self.project = project
        except Project.DoesNotExist:
            raise Http404
        return super(TagsListView, self).get(request,*args, **kwargs)

这是我发现的:

当我像这样定义我的网址时:

url(r"^(\w+)/robjects/samples/$", SampleListView.as_view(), name='sample_list_view'),

所有的人。在我的args中是project_name,我可以通过self.args [0]

来捕获它

但是当我添加时,我的args是空的,

url(r"^(\w+)/robjects/(?P<pk>[0-9]+)/samples/$", SampleListView.as_view(), name='sample_list_view'),

当我定义两个这样的kwargs:

url(r"^(?P<project_name>[\w\-]+)/robjects/(?P<pk>[0-9]+)/samples/list/$",SampleListView.as_view(), name='sample_list_view'),

args empty - 我的预期,kwargs两者都可以并传递给视图

它是一个Django错误还是有任何方法可以使用self.args?他们被kwargs淹没了吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

它看起来像一个bug。我测试了它,当你将args和kwargs传递到你的网址时:

url(r"^(\w+)/test1/(?P<pk>[0-9]+)/test2/$", TestListView.as_view(), name='list_view'),

然后ListView返回args的空元组(它不应该是#t; t)。 当你只将args或kwargs传递给url时它会起作用。