这是我的代码:
urls.py
Item(name=DONE, value=3)
models.py
url(r'^blog/$', views.blog, name='blog'),
views.py
class Category(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=100)
description = models.CharField(max_length=200)
creation_date = models.DateTimeField(auto_now_add=True)
class Post(models.Model):
Status = ((1, "Publicado"), (2, "Borrador"), (3, "Eliminado"))
status = models.IntegerField(choices=Status, default=3)
title = models.CharField(max_length=100)
slug = models.CharField(max_length=100, default='prueba')
description = models.CharField(max_length=200)
content = tinymce_models.HTMLField()
category = models.ForeignKey(Category)
creation_date = models.DateTimeField(auto_now_add=True)
image = models.ImageField(upload_to="photos", default=1)
autor = models.ForeignKey(User)
blog.html
def blog(request):
posts = Post.objects.filter(status=1).values().order_by("-creation_date")
categories = Category.objects.order_by("name")
context = {
"posts":posts,
"categories":categories
}
return render(request, "blog.html", context)
我无法从帖子中访问属性作者和类别。 请。我需要帮助才能做到这一点。 我需要解决这个问题。 非常感谢你。
答案 0 :(得分:0)
尝试在模板中执行此操作:
{% for post in posts %}
<a href="{% url 'detail_post' slug=post.slug %}">{{ post.title }}</a>
<p>{{ post.description }}</p>
{% for category in post.category.all %}
<p>{{ category.name }}</p>
{% endfor %}
{% for author in post.author.all %}
<p>{{ author.name }}</p>
{% endfor %}
<p>{{ post.creation_date }}</p>
{% endfor %}
希望这对你有所帮助。