shreyshrey
shreyshrey

Reputation: 525

Posts not showing author who wrote it in Django

Basically, I'm writing an app in which people can make blog and image posts. So far, I've completed users to be able to write text posts. However, when I try to create a post, it returns "By: None" when it should be returning "By: shrey". In this case, Bob is the author. Here's an image: enter image description here

Here's an image for the post creation view: enter image description here

Theoretically, when I enter a post it should say who it was written by. Here's the template for the create post:

{% extends "social/base.html" %}
{% load crispy_forms_tags %}
{% block content4 %}
  <h1>Make Your Post</h1>
  <p>Write a post / Share an image</p>
  <br>
  <div class="container">
    <form method="post">
      {% csrf_token %}
      {{form|crispy}}

      <button type="submit" name="button">Make Post</button>
    </form>
  </div>
{% endblock content4 %}

Here's the function for the create post view:

class PostCreateView(CreateView):
    model = Posts
    fields = ['post_title', 'post_text_content']

    def form_valid(self, form):
        form.instance.author = self.request.user
        print(self.request.user)
        return super().form_valid(form)

Thank you in advance.

EDIT: Home Page Template (template which displays the posts):

{% extends "social/base.html" %}
{% block content %}
  <h1>Your Feed</h1>
  <p>This is your feed. Here, you'll see posts from people you follow.</p>
  {% if user.is_authenticated %}
    <p>You are logged in as {{user.username}}. This is your feed.</p>
  {% else %}
    <p>You are not logged in. This is a random feed.</p>
  {% endif %}
  {% for post in posts %}
      <h1>{{ post.post_title }}</h1>
      <p>By {{ post.post_author }}  on <i>{{ post.post_date }}</i></p>
      <p>{{ post.post_text_content }}</p>
  {% endfor %}

      <a href="/create">Click here to make a post.</a>
      <br>
      <a href="/logout">Click here to logout.</a>
      <br>
      <a href="/login">Click here to login.</a>
      <br>
      <a href="/register">Click here to sign up and make an account.</a>
  <!--<p>Want to post something? Enter your info here: </p> -->
{% endblock content %}

Posts Model:

class Posts(models.Model):
    post_title = models.CharField(max_length = 40, help_text = 'Enter post title')
    post_text_content = models.TextField(max_length = 1000)
    post_author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    post_date = models.DateField(auto_now = True, auto_now_add = False)
    #Make optional Image Field

    class Meta:
        ordering = ['post_title', 'post_author', 'post_date', 'post_text_content']

    def __str__(self):
        return self.post_title

    def get_absolute_url(self):
        return reverse('social-home')

Upvotes: 1

Views: 320

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476584

The name of the field is post_author, not author, hence you should set post_author:

class PostCreateView(CreateView):
    model = Posts
    fields = ['post_title', 'post_text_content']

    def form_valid(self, form):
        form.instance.post_author = self.request.user
        return super().form_valid(form)

That being said, normally in Django one does not prefixes the model fields with the name of the model. One reason not to do that is that you can define abstract models where you define the field once, and then use inheritance to add the field to other models.

Upvotes: 2

Related Questions