我得到这个错误"没有返回HttpResponse对象。相反,它没有返回”

时间:2022-12-01 20:23:20

I'm trying to have my tags return to a tags detail page but I am getting the following error message

我试着让我的标签返回到标签细节页面,但是我得到了下面的错误信息。

I am getting this error "didn't return an HttpResponse object. It returned None instead"

I tried to copy and modify my "Post" class to suit my "tags" needs but it's not working heres my code

我试图复制和修改我的“Post”类以适应我的“标记”需要,但它在我的代码中不起作用

models.py:

models.py:

class Tag(models.Model):
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=200, unique=True)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("posts:tag_index", kwargs={"slug": self.slug})

    class Meta:
        ordering = ["-timestamp"]

my posts/urls.py:

我的文章/ urls . py:

from django.conf.urls import url

from .views import post_list, post_create, post_detail, post_update, post_delete, sewp, TagIndex


urlpatterns = [
    url(r'^$',   post_list, name='list'),
    url(r'^tag/(?P<slug>[\w-]+)/$', TagIndex, name="tag_index"),
    url(r'^create/$', post_create, name='create'),
    url(r'^sewp$', sewp, name='sewp'),
    url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
    url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),
    url(r'^(?P<id>\d+)/delete/$', post_delete, name='delete'),
]

my views.py:

我的views.py:

def TagIndex(request, slug=None):
    instance = get_object_or_404(Tag, slug=slug)
    context = {
        "instance": instance
    }
    render(request, "posts/tags.html", context)

my tags.html:

我的tags.html:

{% extends 'posts/base.html' %}

{% block content %}

   {{ instance }}

{% endblock content %}

I saw a video on youtube where the the guy did

我在youtube上看到一个视频,是那个家伙做的

def detail(request):
   message = "hello"
   return HttpResponse(message)

but that doesn't suit my needs. How can I make it so when I click a link all posts with the same link are shown on my tags.html page?

但那不符合我的需要。当我点击一个链接时,标签上会显示所有有相同链接的文章。html页面吗?

1 个解决方案

#1


4  

you should return render

你应该返回渲染

return render(request, "posts/tags.html", context)

返回渲染(请求,“文章/标记。html”,上下文)

Render is a shortcut for rendering a template with a context and returning an HttpResponse

呈现是使用上下文呈现模板并返回HttpResponse的快捷方式

#1


4  

you should return render

你应该返回渲染

return render(request, "posts/tags.html", context)

返回渲染(请求,“文章/标记。html”,上下文)

Render is a shortcut for rendering a template with a context and returning an HttpResponse

呈现是使用上下文呈现模板并返回HttpResponse的快捷方式