Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7936727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:18:06+00:00 2026-06-03T22:18:06+00:00

First off, I’m sure this is a simple question. I’m just getting started with

  • 0

First off, I’m sure this is a simple question. I’m just getting started with Django and as all beginners do, I thought I’d build a simple blog.

I have a simple data model, a Post model that contains with a link to a user via a FK.

models.py

class Post(TimeStampedActivate):
    """
    A blog post
    """
    title = models.CharField(max_length=255)
    slug = models.SlugField()
    excerpt = models.TextField(blank=True)
    body = models.TextField()
    publish_at = models.DateTimeField(default=datetime.datetime.now())
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    active = models.BooleanField(default=False, help_text='Is the blog post active?')
    user = models.ForeignKey(User, related_name='post_user')

    def __unicode__(self):
        return self.title

I then want a page that lists all of the Posts alongside the username of the person who created the post. My current view looks like this:

views.py

def index(request):
    posts = Post.objects.filter(active=True)   
    user = User.objects.get(id=1)
    return render(request, 'blog/index.html', {'posts': posts, 'user': user})

My template
At present this just displays the users name that matches an ID of 1.

{% for post in posts %}
    <h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
    <p>{{ post.excerpt|truncatewords:30 }}</p>
    <p>Posted by {{ user.first_name }} {{ user.last_name }}</p>
{% endfor %}

How would I modify my views.py file to ensure I get the first and last name of the user responsible for the post?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:18:08+00:00Added an answer on June 3, 2026 at 10:18 pm

    View:

    def index(request):
        posts = Post.objects.filter(active=True)   
        return render(request, 'blog/index.html', {'posts': posts})
    

    Template:

    {% for post in posts %}
        <h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
        <p>{{ post.excerpt|truncatewords:30 }}</p>
        <p>Posted by {{ post.user.first_name }} {{ post.user.last_name }}</p>
    {% endfor %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, thank you to everyone on this site...it's been INCREDIBLY helpful in getting
first off this is a class assignment so i would appreciate help but just
First off, please accept my apologies if this question is basic, I mainly have
First off, apologies if this is a duplicate of an existing question. Wasn't precisely
First off, I'd love to post some real code for this question, but I
First off, this is the error I am getting java.lang.OutOfMemoryError at coderaustin.com.FileEncryptor.encryptFile(FileEncryptor.java:56) at coderaustin.com.Main.onCreate(Main.java:41)
First off, apologies if this is a complete noob question which could easily be
First off - apologies if this or a similar question has been asked before.
First off, this question is ripped out from this question. I did it because
First off all I didn't find better title for my question, so please update

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.