【问题标题】:get() got an unexpected keyword argument 'title'get() 得到了一个意外的关键字参数 'title'
【发布时间】:2021-10-25 20:02:18
【问题描述】:

我想获取一个具有唯一模型字段的博客,但是当我点击一个特定的博客时,它会抛出我上面提到的错误 这是我的看法

class Blogs(View):
    def get(self, request):
        blog_list = Blog.objects.order_by('-joined_date')
        return render(request, 'blogs.html',{'blog_list':blog_list})

class ReadBlogs(View):
def get(self, request, title):
    readblog = Blog.objects.filter(title=title)
    return render(request,'blogs_read.html', {'readblog':readblog})

我的模特

class Blog(models.Model):
    title  = models.CharField(max_length=48, blank=False)
    urltitle = models.SlugField(max_length=48, blank=False, unique=True)
    title_image = models.ImageField(upload_to='blog',blank=True, null=True)
    subone = models.CharField(max_length=80, blank=False)
    subone_image = models.ImageField(upload_to='blog',blank=True,null=True)
    onedes = models.TextField(blank=False)

我的 html 用于获取正确的博客

<div class="blogs">
            {% for blogs in blog_list %}
            <a class="products" href="{% url 'blogs:readblog' title=blogs.urltitle %}">
              <div class="blog col-4" style="width: 18rem; height:350px">
                  <img class="img" src="{{ blogs.title_image.url }}" alt="" height="250px" width="100%">
                  <div class="detail">
                  <h4 class="title text-center" style="color: #025; font-family:cursive;">{{blogs.title}}</h4>
                  </div>
                </div>
              </a>
            {% endfor %}
          </div>

我的 url.py

urlpatterns = [
    path('blogs/',Blogs.as_view(),name="Blogs"),
    path('<slug:title>/',ReadBlogs.as_view(),name="readblog")
]

你可以看到 mu urltitle 是唯一的 slug 字段,所以但是当我点击一个特定的博客时,我得到了上面提到的错误,不知道是什么导致了错误

我用于显示该博客相关字段的模板

<div class="col-11 card">
        <div class="blogs">
        {% for blog in readblog %}
          
          <h1 class="text-center" style="color: #025; font-family:cursive; margin-top:20px;">{{read.title}}</h1>
          
          {% endfor %}
        </div>
      </div>

【问题讨论】:

    标签: django django-models django-views django-urls django-class-based-views


    【解决方案1】:

    URL参数命名为title,而不是url_title

    path('&lt;slug:<strong>title</strong>&gt;/',ReadBlogs.as_view(),name='readblog')

    因此.get(…) 方法应该使用title 作为参数:

    class ReadBlogs(View):
        
        #                title &downarrow;
        def get(self, request, title):
            blog = Blog.objects.filter(title=title)
            return render(request,'blogs_read.html',{'blog':blog})

    这里的blog 是一个集合,包含零个、一个或多个博客。如果你想传递一个单个Blog对象,你应该获取一个对象,例如get_object_or_404

    from django.shortcuts import get_object_or_404
    
    class ReadBlogs(View):
    
        def get(self, request, title):
            blog = get_object_or_404(Blog, title=title)
            return render(request,'blogs_read.html',{'blog':blog})

    使用DetailView [Django-doc] 自动正确呈现项目也可能更有意义:

    from django.shortcuts import get_object_or_404
    from django.views.generic.detail import DetailView
    
    class ReadBlogs(DetailView):
        model = Blog
        template_name = 'blogs_read.html'
    
        def get_object(self, *args, **kwargs):
            return get_object_or_404(Blog, title=self.kwargs['title'])

    【讨论】:

    • 但是当我尝试获取该博客的真实数据时,我的模板仍然没有显示任何内容
    • @Blackranger:链接应该是{% url 'blogs:readblog' title=blogs.title %} 所以blogs.title,而不是blogs.urltitle,没有urltitle(或者至少没有你在这里显示的)。
    • @Blackranger:此外,最后一个代码片段应该是{{blog.title}},而不是{{ read.title }}
    • 我想在我的 url 中使用 urltitle slug 字段
    • 这也在模型中
    【解决方案2】:

    模板

    {% url 'blogs:readblog' title=blogs.urltitle %}
    

    这里,你传递了title=urltitle,所以你实际上传递了urltitle

    观看次数

    # your-code | wrong
    class ReadBlogs(View):
        def get(self, request, title):
            # ======== HERE, TITLE IS ACTUALLY urltitle! ==================
            readblog = Blog.objects.filter(title=title)
            return render(request,'blogs_read.html', {'readblog':readblog})
    
    
    # correct
    class ReadBlogs(View):
        def get(self, request, title):
            readblog = Blog.objects.filter(urltitle = title)
            return render(request,'blogs_read.html', {'readblog':readblog})
    

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 2021-08-05
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 2016-09-17
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多