Answer the question
In order to leave comments, you need to log in
Why is the author field left blank?
I'm creating a small blog in Django. I made it so that each user could create his own post and when viewing the post, the username was displayed. However, when creating a post through the site, the author field remains empty, while in the admin panel in the post settings it is possible to select the author of the post. What's my mistake?
models.py
class Post(models.Model):
theme = models.CharField(max_length=100)
post_text = models.CharField(max_length=4000)
pub_date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(
User,
on_delete=models.CASCADE,
)
def get_absolute_url(self):
return reverse("Blog:detail", kwargs={'pk': self.pk})
def __str__(self):
return self.theme
class IndexView(generic.ListView):
queryset = Post.objects.all()
template_name = 'Blog/index.html'
context_object_name = 'latest_post_list'
class DetailView(generic.DetailView):
template_name = "Blog/detail_post.html"
queryset = Post.objects.all()
class AboutView(generic.TemplateView):
template_name = 'Blog/about.html'
class CreateView(generic.CreateView):
model = Post
template_name = 'Blog/create_post.html'
fields = ['theme',
'post_text']
{% extends 'main.html' %}
{% block content %}
<h1>{{ post.theme }}</h1>
<li>{{ post.post_text }}</li>
<li>{{ post.pub_date }}</li>
<li>Author: {{ post.author }}</li>
{% endblock %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question