简体   繁体   English

Django CBV与uuid作为参数

[英]Django CBV with uuid as parameter

I'm new to Django and while I'm okay with FBVs, I'm trying to learn CBVs and I'm stuck at one point. 我是Django的新手,虽然我对FBV很好,但我正在努力学习CBV,而且我一度陷入困境。 I have this in urls.py: 我在urls.py中有这个:

uuid='\w\w\w\w\w\w\w\w-\w\w\w\w-\w\w\w\w-\w\w\w\w-\w\w\w\w\w\w\w\w\w\w\w\w'

url(r'^someroute/$', MyView.as_view(), name="my_list_view"),
url(r'^someroute/(?P<uuid>%s)/$' % uuid, MyDetailView.as_view(), name="my_detail_view"),

The first one works just fine. 第一个工作正常。 It extends ListView. 它扩展了ListView。 The second one extends DetailView and I get this error when I click on a properly rendered link with uuid: 第二个扩展了DetailView,当我点击使用uuid的正确渲染链接时,我收到此错误:

Generic detail view MyDetailView must be called with either an object pk or a slug.

I browsed the net for a solution, encountered several of them, but nothing seemed to solve my issue. 我浏览网络寻找解决方案,遇到其中几个,但似乎没有解决我的问题。 This is MyDetailView (so far I just want it to render an empty template, then I'll continue): 这是MyDetailView(到目前为止我只想让它呈现一个空模板,然后我会继续):

class MyDetailView(DetailView):
    model = MyModel
    template_name = 'someroute/my_detail.html'
    slug_filed = 'uuid' #here I tried several variations, but nothing

Instead of slug_field, I also tried: 我没有尝试过slug_field,而是尝试了:

pk_url_kwarg = 'uuid'

But then I get this error: invalid literal for int() with base 10: '1c149e4a-8629-464f-9c62-2742f82e6d96' 但后来我得到了这个错误:对于基数为10的int()的无效文字:'1c149e4a-8629-464f-9c62-2742f82e6d96'

What am I doing wrong? 我究竟做错了什么?

class MyDetailView(DetailView):
    slug_url_kwarg = 'uuid'
    slug_field = 'uuid'

    model = MyModel
    template_name = 'someroute/my_detail.html'

属性是slug_field (不是slug_filed)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM