如何在具有相同for循环的两个子类模型上进行迭代?

时间:2019-04-24 14:07:59

标签: django django-models django-templates django-model-utils

我想创建一个照片共享网站,但我不知道如何遍历 此代码:

all_post = Images.objects.all().order_by('-created').select_subclasses()

我的模型。py

class Images(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='images_created', on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
    objects = InheritanceManager()

class Postimg(Images):
    user_img= models.ImageField(upload_to='images/%Y/%m/%d', null=True, blank=True)


class Postsms(Images):
    user_message = models.CharField(max_length=500,blank=True)

views.py

@login_required
def home(request):
    all_post = Images.objects.all().order_by('-created').select_subclasses()
    return render(request, 'copybook/home.html',{ 'all_post':all_post})

home.html

  {% for foo in all_post %}

      <hr>
     <br> SMS by:  <p>{{ foo.user}}  __  {{ foo.created }}</p> <br>
      <h2>{{ foo.user_message }}</h2>

       <hr>
   <br> IMAGE by:  <p>{{ foo.user}}  __  {{ foo.created }}</p> <br>

   <img src="{{ foo.user_img.url }}"width="250">

  {% endfor %}

我如何在home.html中对其进行迭代

0 个答案:

没有答案