Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What's causing the tuple and get error in the django?

I am learning from the book Django3 by example. I am having a problem and I am unable to debug it. I am unable to understand what does this mean?

I am displaying list of posts on a page. This error happens when I click the particular post to view whole of the blog post.

Here is my error: –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Traceback (most recent call last):
  File "/mnt/g/Programming/django/blog/my_env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/mnt/g/Programming/django/blog/my_env/lib/python3.8/site-packages/django/utils/deprecation.py", line 96, in __call__
    response = self.process_response(request, response)
  File "/mnt/g/Programming/django/blog/my_env/lib/python3.8/site-packages/django/middleware/clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:

Exception Type: AttributeError at /blog/2022/1/19/what-is-model/
Exception Value: 'tuple' object has no attribute 'get'

my views.py

from django.shortcuts import render, get_object_or_404
from .models import Post

# Create your views here.

    def post_lists(request):
        posts = Post.published.all()
        return render(request, 'blog/post/list.html', { 'posts': posts})
    
    def post_detail(request, year, month, day, post):
        post = get_object_or_404(Post, slug = post, status='published', publish__year=year, publish__month=month, publish__day=day)
        return(request, 'blog/post/detail.html', {'post': post})

My models.py

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse

# Create your models here.

class PublishedManager(models.Manager):
    def get_queryset(self):
        return super(PublishedManager, self).get_queryset().filter(status='published')    

class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published')
    )

    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique_for_date='publish')
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=50, choices=STATUS_CHOICES, default='draft')

    def get_absolute_url(self):
        return reverse('blog:post_detail', args=[self.publish.year, self.publish.month, self.publish.day, self.slug])
    

    # managers
    objects = models.Manager()
    published = PublishedManager()

    class Meta:
        ordering = ('-publish',)

    def __str__(self):
        return self.title
    

>Solution :

The problem is in post_detail. You return a tuple instead of a rendered page

return(request, ‘blog/post/detail.html’, {‘post’: post})

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading