Django + HTML 模板无法正确显示
Django + HTML Template cannot display the right way
I'like to represent the list of news_list, the result should be like following style using 2 loops:
category1
-- info_record 1
-- info_record 2
category2
-- info_record 3
The problem is, inside loop {% for m in p_list.p.name %} always nothing happen, meanwhile outside loop {% for p in p_category %} could be retrieved properly.
html模板文件如下所示,
{% block content %}
<div class = "container">
{% for p in p_category %}
<div class = "row" >
......... {{p}}
</br>
</div>
{% for m in p_list.p.name %}
<div calss ="row">
.. <a href = "/pubinfo/{{m.slug}}" > {{m.title}} - [ {{m.created}}] </a>
</div>
{% endfor %}
{% endfor %}
</div>
{% endblock %}
查看文件如下图,
from django.shortcuts import render
from datetime import datetime
from .models import Category
from .models import PostInfo
def list_all(request):
post_categories = Category.objects.all()
post_lists ={}
for cate in post_categories:
post_lists[cate.name] = PostInfo.objects.all().filter(category = cate)
import pdb; pdb.set_trace()
return render(request, 'pub_post.html', {'p_category': post_lists.keys(), "p_list": post_lists})
模型文件如下所示,
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=200,
db_index=True)
slug = models.SlugField(max_length=200,
db_index=True,
unique=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
class PostInfo(models.Model):
title = models.CharField(max_length=200,
db_index=True)
slug = models.SlugField(max_length=200,
db_index=True,
unique=True)
content = models.TextField(blank=True,
null=True)
category = models.ForeignKey(Category,
related_name='post_category')
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, related_name='created_by_user')
updated_by = models.ForeignKey(User, related_name='updated_by_user')
class Meta:
ordering = ('updated',)
# verbose_name = 'post'
# verbose_name_plural = 'posts'
def __str__(self):
return self.title
感谢任何人能伸出援手,再次感谢。
那是因为 p_list 是字典。
试试这个:
{% for key, value in p_list.items %}
{% if key == p %}
{% for m in value %}
<div calss ="row">
.. <a href = "/pubinfo/{{m.slug}}" > {{m.title}} - [ {{m.created}}] </a>
</div>
{% endfor %}
{% endif %}
{% endfor %}
.items 将字典转换为(键,值)对列表
更新,你可以用这个替换整个模板并避免上下文变量p_category
{% block content %}
<div class = "container">
{% for p, values in p_list.items %}
<div class = "row" >
......... {{p}}
</br>
</div>
{% for m in values %}
<div calss ="row">
.. <a href = "/pubinfo/{{m.slug}}" > {{m.title}} - [ {{m.created}}] </a>
</div>
{% endfor %}
{% endfor %}
</div>
{% endblock %}
I'like to represent the list of news_list, the result should be like following style using 2 loops: category1 -- info_record 1 -- info_record 2 category2 -- info_record 3
The problem is, inside loop {% for m in p_list.p.name %} always nothing happen, meanwhile outside loop {% for p in p_category %} could be retrieved properly.
html模板文件如下所示,
{% block content %}
<div class = "container">
{% for p in p_category %}
<div class = "row" >
......... {{p}}
</br>
</div>
{% for m in p_list.p.name %}
<div calss ="row">
.. <a href = "/pubinfo/{{m.slug}}" > {{m.title}} - [ {{m.created}}] </a>
</div>
{% endfor %}
{% endfor %}
</div>
{% endblock %}
查看文件如下图,
from django.shortcuts import render
from datetime import datetime
from .models import Category
from .models import PostInfo
def list_all(request):
post_categories = Category.objects.all()
post_lists ={}
for cate in post_categories:
post_lists[cate.name] = PostInfo.objects.all().filter(category = cate)
import pdb; pdb.set_trace()
return render(request, 'pub_post.html', {'p_category': post_lists.keys(), "p_list": post_lists})
模型文件如下所示,
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=200,
db_index=True)
slug = models.SlugField(max_length=200,
db_index=True,
unique=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
class PostInfo(models.Model):
title = models.CharField(max_length=200,
db_index=True)
slug = models.SlugField(max_length=200,
db_index=True,
unique=True)
content = models.TextField(blank=True,
null=True)
category = models.ForeignKey(Category,
related_name='post_category')
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, related_name='created_by_user')
updated_by = models.ForeignKey(User, related_name='updated_by_user')
class Meta:
ordering = ('updated',)
# verbose_name = 'post'
# verbose_name_plural = 'posts'
def __str__(self):
return self.title
感谢任何人能伸出援手,再次感谢。
那是因为 p_list 是字典。
试试这个:
{% for key, value in p_list.items %}
{% if key == p %}
{% for m in value %}
<div calss ="row">
.. <a href = "/pubinfo/{{m.slug}}" > {{m.title}} - [ {{m.created}}] </a>
</div>
{% endfor %}
{% endif %}
{% endfor %}
.items 将字典转换为(键,值)对列表
更新,你可以用这个替换整个模板并避免上下文变量p_category
{% block content %}
<div class = "container">
{% for p, values in p_list.items %}
<div class = "row" >
......... {{p}}
</br>
</div>
{% for m in values %}
<div calss ="row">
.. <a href = "/pubinfo/{{m.slug}}" > {{m.title}} - [ {{m.created}}] </a>
</div>
{% endfor %}
{% endfor %}
</div>
{% endblock %}