get() 得到了一个意外的关键字参数 'title'

get() got an unexpected keyword argument 'title'

我想获取一个带有唯一模型字段的博客,但是当我点击一个特定的博客时,它会抛出上述错误 这是我的观点

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>

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

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

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

class ReadBlogs(View):
    
    #                title ↓
    def get(self, request, <strong>title</strong>):
        blog = Blog.objects.filter(title=title)
        return render(request,'blogs_read.html',{'blog':blog})

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

from django.shortcuts import <strong>get_object_or_404</strong>

class ReadBlogs(View):

    def get(self, request, title):
        blog = <strong>get_object_or_404(</strong>Blog, title=title<strong>)</strong>
        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 <strong>DetailView</strong>

class ReadBlogs(<strong>DetailView</strong>):
    model = Blog
    template_name = 'blogs_read.html'

    def get_object(self, *args, **kwargs):
        return get_object_or_404(Blog<strong>, title=self.kwargs['title']</strong>)

模板

{% 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})