losee
losee

Reputation: 2238

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

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

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:

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:

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:

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

{% block content %}

   {{ instance }}

{% endblock content %}

I saw a video on youtube where the the guy did

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?

Upvotes: 1

Views: 779

Answers (1)

dm03514
dm03514

Reputation: 55972

you should return render

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

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

Upvotes: 4

Related Questions