首页 文章

得到“ValueError:对于基数为10的int()的无效文字:'Trancel'”在Django的detailview中使用两个参数时

提问于
浏览
0

我试图在详细视图中使用除了pk和slug之外的其他两个url标签 . 我有以下代码片段:

views.py

    class UseCaseDetailView(DetailView):
        template_name = "useCaseExtract/useCaseDetail.html"
        model = UseCaseProfile
        context_object_name = 'usecaseprofile'
        '''I am trying to override the get_object method of DetailView to accept url tags project and usecasename instead of pk and slug'''
        def get_object(self):     
            obj = get_object_or_404(UseCaseProfile, project=self.kwargs['project'], useCasename=self.kwargs['useCasename'])
            return obj

urls.py
...

    path('/UseCaseDetail/', UseCaseDetailView.as_view(), name='UseCaseDetail')

...

in my template file, I have the following link:
/Trancel/UseCaseDetail/Nothing

after I clicked the link, I got:
ValueError: invalid literal for int() with base 10:  'Trancel'

can you some please explain to me why I am getting this error?

2 回答

  • 0

    你有能力将 Trance1 重命名为 TranceOne 吗?这样它就不包含整数?

    我认为你的问题是 1 什么时候解析URL ..(因为它期待一个刺痛,但它得到一个字符串w / 1 ) .

  • 0

    项目url标记是UseCaseProfile模型中的外键 . 为了查询UseCaseProfile,我在detailview中修改了get_object代码,如下所示 .

    def get_object(self):
            project=Project.objects.all()
            x=project.filter(projectName=self.kwargs['project'])
            obj = get_object_or_404(UseCaseProfile, project=x.first(), useCasename=self.kwargs['useCasename'])
            return obj
    

相关问题