Django tutorial getting stuck at fixing the bug topic

I was following tutorial https://docs.djangoproject.com/en/4.0/intro/tutorial05/ but when i get to fixing the bug, the bug should be resolved after updating models.py but even after i updated it it didnt fix. I copied the codes so i dont mess up but still didnt fixed it

Please post your current complete Question model.

When posting code, copy / paste the code into the body of your message, between lines of three backtick - ` characters. You’ll have a line of ```, then your code, then another line of ```. Please do not post images of code.

models.py
β€˜β€™β€™
import datetime

from django.db import models

from django.utils import timezone

Create your models here.

class Question(models.Model):

question_text = models.CharField(max_length=200)

pub_date = models.DateTimeField('date published')

def __str__(self):

    return self.question_text

def was_published_recently(self):

    return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

class Choice(models.Model):

question = models.ForeignKey(Question, on_delete=models.CASCADE)

choice_text = models.CharField(max_length=200)

votes = models.IntegerField(default=0)

def __str__(self):

    return self.choice_text

def was_published_recently(self):

now = timezone.now()

return now - datetime.timedelta(days=1) <= self.pub_date <= now

β€˜β€™β€™
test.py
β€˜β€™β€™
import datetime

from django.test import TestCase

from django.utils import timezone

from .models import Question

class QuestionModelTests(TestCase):

def test_was_published_recently_with_future_question(self):

    """

    was_published_recently() returns False for questions whose pub_date

    is in the future.

    """

    time = timezone.now() + datetime.timedelta(days=30)

    future_question = Question(pub_date=time)

    self.assertIs(future_question.was_published_recently(), False)

β€˜β€™β€™

The characters surrounding your code should be backticks - `, not apostrophes - '.

models.py

import datetime
from django.db import models
from django.utils import timezone

# Create your models here.
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):
        return self.question_text
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    
class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):
        return self.choice_text
    
def was_published_recently(self):
    now = timezone.now()
    return now - datetime.timedelta(days=1) <= self.pub_date <= now

tests.py

import datetime

from django.test import TestCase
from django.utils import timezone

from .models import Question


class QuestionModelTests(TestCase):

    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=time)
        self.assertIs(future_question.was_published_recently(), False)

Ok, so the issue here is that you did not fix the was_published_recently method within your Question model, you added a second copy of the method - and it’s not even in that particular class.

1 Like