Search code examples
pythondjangopython-3.xmodelsstrftime

ValueError at / Invalid format string


enter image description herei am making a blog website through Django! While adding the strftime for dates in my project, I am getting Value error! Here are the code in which i get error: models.py

from django.db import models

class Blog(models.Model):
    title = models.CharField(max_length=200)
    pub_date = models.DateTimeField()
    body = models.TextField()
    image = models.ImageField(upload_to='images/')

    def summary(self):
        return self.body[:100]

    def pub_date_pretty(self):
        return self.pub_date.strftime('%b %e %Y')

Here are the HTML code in which i get error:

 {% for blog in blogs.all %}
   <a href=""><h3 >{{ blog.title }}</h3></a>
    {{ blog.pub_date_pretty }}
<br />
    <img class="img-fluid" src="{{ blog.image.url }}" height =200 width=200 />
    <br />
    <p>{{ blog.summary }}</p>
{% endfor %}

Solution

  • The problem is this call:

    self.pub_date.strftime('%b %e %Y')
    

    %e is not a valid directive for strftime. If you just want to display the date, use something like this:

    self.pub_date.strftime('%b %d %Y')
    

    You can see a full list of the directives you can use here