ValueError at /author/admin/ in Django

211 views Asked by At

I have the following blog project :

urls.py conf :

  url(r'^author/(?P<author>\w+)/$', views.getAllPosts, name='grabAuthorPosts')

posts/models:

class Post(models.Model):
    title = models.CharField(max_length=200)
    summary = models.CharField(max_length=500, default = True)
    body = models.TextField()
    pub_date = models.DateTimeField(default=timezone.now)
    category = models.ManyToManyField('Category')
    author = models.ForeignKey(User, default=True)
    slug = models.SlugField(max_length=100, unique=True, blank=True)

    def __str__(self):
        return self.title

    def slug(self):
        return slugify(self.title)

posts/views:

def getAllPosts(request, author=False):
    latest_posts = Post.objects.all().order_by('-pub_date')
    comments = Comment.objects.all().order_by('-pub_date')
    author_posts = User.objects.get(id=author)
    author_posts = author_posts.post_set.all()

    context = {
        'latest_posts':latest_posts,
        'comments':comments,
        'author_posts':author_posts
    }
    return render(request, 'posts/getAllPosts.html', context)

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=500, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)

templates/posts/getAllPosts:

<a href={% url 'grabAuthorPosts' author=post.author.username %}>
{{post.author}}</a>

I am trying to make it so that when the post.author link is clicked, the user will be taken to a page consisting of posts related to that particular author. The link formulation itself seems ok, as when clicked on a post created by admin, the url reads localhost/author/admin/

I believe my problem is in getting the context variable author_posts to work. I'm new to Django so any explanation greatly appreciated.

latest_posts, as well as author=False is used elsewhere in the template to get all posts regardless of author, which works fine.

The error is :

ValueError at /author/admin/
invalid literal for int() with base 10: 'admin'
0

There are 0 answers