1. 查询数据并展示
1.查询分类文章数据并通过context传递给HTML
from home.models import ArticleCategory
class WriteBlogView(LoginRequiredMixin,View):
def get(self,request):
# 获取博客分类信息
categories = ArticleCategory.objects.all()
context = {
'categories': categories
}
return render(request,'write_blog.html',context=context)
2.在write_blog.html文件中使用模板语言展示数据
<!-- 文章栏目 -->
<div class="form-group">
<label for="column">栏目</label>
<select class="form-control col-3" id="category" name="category">
{% for category in categories %}
<option value="{{ category.id }}">{{ category.title }}</option>
{% endfor %}
</select>
</div>
效果展示: