{{ post.title }}
{{ post.content|truncatewords:30 }}
{{ post.created_at|date:"Y/m/d" }}صفحه اصلی معمولاً اولین جایی هست که کاربر میبینه. باید جذاب، سریع و پر از اطلاعات مفید باشه!
# blog/views.py
from django.shortcuts import render
from .models import Post, Category
def index(request):
latest_posts = Post.objects.filter(published=True)[:10]
categories = Category.objects.all()
context = {
'latest_posts': latest_posts,
'categories': categories,
'total_posts': Post.objects.filter(published=True).count()
}
return render(request, 'blog/index.html', context)
{% extends "blog/base.html" %}
{% block title %}صفحه اصلی{% endblock %}
{% block content %}
به وبلاگ من خوش آمدید!
تعداد کل پستها: {{ total_posts }}
آخرین پستها
{% for post in latest_posts %}
{{ post.title }}
{{ post.content|truncatewords:30 }}
{{ post.created_at|date:"Y/m/d" }}
{% endfor %}
دستهبندیها
{% for category in categories %}
- {{ category.name }}
{% endfor %}
{% endblock %}
یک صفحه اصلی کامل بسازید که شامل بخش hero، آخرین پستها، دستهبندیهای محبوب و آمار سایت (تعداد پستها، دستهبندیها و...) باشه.
این درس را به پایان رساندید و میتوانید به درس بعدی بروید.