[英]How can get Post data form Category in Django?
I tried using Django a bit.我尝试使用 Django 一点。 Now I'm stuck with posting issues in the category.现在我坚持在该类别中发布问题。 I want to know how we can use it.我想知道我们如何使用它。 Are there any filters that I should use?有什么我应该使用的过滤器吗?
Code in my model.py我的 model.py 中的代码
class Category(models.Model):
title = models.CharField(max_length=255, verbose_name="ชื่อหมวดหมู่")
content = models.TextField(default='ใส่เนื้อหาบทความ')
slug = models.SlugField(max_length=200, default='ใส่ลิงค์บทความ ตัวอย่าง /your-post-content')
parent = models.ForeignKey('self',blank=True, null=True ,related_name='children',on_delete=models.CASCADE)
class Meta:
unique_together = ('slug', 'parent',)
verbose_name_plural = "categories"
def __str__(self):
full_path = [self.title]
k = self.parent
while k is not None:
full_path.append(k.title)
k = k.parent
return ' -> '.join(full_path[::-1])
class Post(models.Model):
id = models.AutoField
category = models.ForeignKey('Category',on_delete=models.CASCADE)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
content_images = models.ImageField(default='media/noimg-l.jpg')
title = models.CharField(max_length=200,unique=True,default='ใส่ชื่อบทความ')
content = models.TextField(default='ใส่เนื้อหาบทความ')
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
post_viewcount = models.PositiveIntegerField(default=0,)
slug = models.SlugField(max_length=200, default='ใส่ลิงค์บทความ ตัวอย่าง /your-post-content')
status = models.IntegerField(choices=STATUS , default=1)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
def get_cat_list(self):
k = self.category # for now ignore this instance method
breadcrumb = ["dummy"]
while k is not None:
breadcrumb.append(k.slug)
k = k.parent
for i in range(len(breadcrumb)-1):
breadcrumb[i] = '/'.join(breadcrumb[-1:i-1:-1])
return breadcrumb[-1:0:-1]
and my view.py和我的观点.py
def cat_list(request):
categories = Category.objects.all()
return render(request, 'blog/categories.html', {'categories': categories})
def category_detail(request, slug):
category = get_object_or_404(Category, slug=slug)
return render(request, 'blog/category_detail.html', {'category': category})
and my urls.py和我的 urls.py
urlpatterns = [
path('', views.post_list, name='post_list'),
path('createcontent/', views.create_post, name='create_post'),
path('article/<slug:slug>/', views.post_detail, name='post_detail'),
path('category/', views.cat_list, name='category'),
path('category/<slug:slug>/', views.category_detail, name='category_detail'),
]
and last on in my template最后在我的模板中
<h1> {{ categories.title }} </h1>
<div class="container">
<div class="row">
{% for post in categories %}
<div class="column">
<div class="card" style="width: 20rem;">
<img class="card-img-top" src="{{ post.content_images.url }}" alt="{{ post.title }}">
<div class="card-body">
<b><p class="card-text"> <a href="{% url 'post_detail' post.slug %}"> {{ post.categories.title }} </a></p></b>
<p> เขียนโดย {{ post.author }}</p>
<p class="card-text"> {{ post.content|safe|slice:":200" }}</p>
<div align="center"><button type="button" class="btn btn-outline-secondary"> <a href="{% url 'post_detail' post.slug %}"> อ่านเรื่องนี้ต่อ </a></button> </div>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
I try to find information on how to do it on the website Djangogirl and others because I want to have my own small blog project.我尝试在 Djangogirl 和其他网站上查找有关如何操作的信息,因为我想拥有自己的小型博客项目。 Don't want to use Wordpress不想用 Wordpress
In your detail view, you pass a single Category
object with the name category
, not .在您的详细视图中,您传递了一个名称为categories
category
的Category
object ,而不是 。 You can iterate over the posts with categories
{% for post in category.post_set.all %}
:您可以使用{% for post in category.post_set.all %}
遍历帖子:
<h1> {{ category.title }} </h1>
<div class="container">
<div class="row">
{% for post in category.post_set.all %}
<div class="column">
<div class="card" style="width: 20rem;">
<img class="card-img-top" src="{{ post.content_images.url }}" alt="{{ post.title }}">
<div class="card-body">
<b><p class="card-text"> <a href="{% url 'post_detail' post.slug %}"> {{ post.categories.title }} </a></p></b>
<p>{{ post.author }}</p>
<p class="card-text"> {{ post.content|safe|slice:":200" }}</p>
<div align="center"><button type="button" class="btn btn-outline-secondary"> <a href="{% url 'post_detail' post.slug %}"></a></button> </div>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
A ForeignKey
[Django-doc] , django makes a manager in the " target model " to query in the opposite direction.一个ForeignKey
[Django-doc] , django 在“目标 model ”中创建一个经理以相反的方向查询。 If you do not specify the related_name=…
parameter [Django-doc] , the related name is the name of the "target model" in lowercase, and a _set
suffix (here post_set
), so that is the way how you can query the related Post
s for a given Category
.如果不指定related_name=…
参数 [Django-doc] ,则相关名称是小写的“目标模型”的名称,以及一个_set
后缀(这里是post_set
),这样就可以查询给定Category
的相关Post
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.