从for循环模板Django上的多个字段访问date_subscribed

时间:2017-08-28 15:47:39

标签: django loops templates manytomanyfield m2m

我有这些模型,

class Place(models.Model):
   name = models.CharField(max_length=200)
   user = models.ForeignKey(User, on_delete=models.CASCADE, 
   related_name='places', default=1)
   subscribers = models.ManyToManyField(AppUser, through='PlaceSubscriber')
   def __str__(self):              # __unicode__ on Python
       return self.name

class PlaceSubscriber(models.Model):
    place = models.ForeignKey(Place, on_delete=models.CASCADE)
    user = models.ForeignKey(AppUser, on_delete=models.CASCADE)
    date_subscribed = models.DateTimeField(editable=False, default=timezone.now)

    class Meta:
        unique_together = ('place', 'user')

我想在我的模板

中访问此for循环的date_subscribed字段
{% for o in place.subscribers.all %}
    <a href="#" class="list-group-item clearfix">
    <span class="pull-left">
         {{ forloop.counter }}. &emsp;
    </span>
    <span class="pull-left">
         <strong>{{ o.full_name }}</strong>
         <p>Email: <i>{{ o.email }}</i> | Date Subscribed: <i> {{ 
          o.place__placesubscriber__date_subscribed }} </i> </p>
    </span>
    <span class="pull-right">
    <span class="btn btn-xs btn-primary" 
       onclick="sendPushNotificationToUser('{{ o.ionic_id }}'); return 
       false;">Send Message</span>
    <span class="btn btn-xs btn-danger" onclick="deletePlaceUser({{ place.id 
        }}, {{ o.id }}); return false;  ">
      Unsubscribe
   </span>

   </button>
   </span>
   </a>
{% endfor %}

我可以像这样访问此foorloop之外的date_subscribed字段:

{% for each in place.placesubscriber_set.all %}
    {{ each.date_subscribed }}
{% endfor %}

但还没弄清楚如何在另一个内部访问它。

更新

这是我的观点

class PlaceDetailView(DetailView):
    model = Place
    template_name = 'place/place_detail.html'

这是网址格式

url(r'^place/(?P<pk>\d+)$', views.PlaceDetailView.as_view(), name='detail_place'),

3 个答案:

答案 0 :(得分:0)

我认为在你的views.py文件中你应该这样做:

from django.shortcuts import render
from .models import Place
def placeview(request):
    place = Place()
    return render(request, 'place.html', {'place':place})

答案 1 :(得分:0)

我经常尝试在模板中处理简单的数据结构,以避免复杂的逻辑。

我会将视图替换为一个简单的视图,以便进行所需的查询(希望如此):

def place_detail(request):

    data = PlaceSubscriber.objects.all().values(
        'user__full_name',
        'user__email',
        'place__name',
        'date_subscribed'
    )

    return render(request,'place.html',{'data':data})

这将生成带有查询键的字典列表。您现在可以通过以下方式显示此数据:

{% for el in data %}
    {el.user__full_name}
    {el.date_subscribed}
    ...
{% endfor %}

好吧,我不确定你是不是想要,但是你可以改变查询值来得到你想要的。

希望它有所帮助。

答案 2 :(得分:0)

我结束了@Tico的方法,我更新了我的视图,因此它的逻辑比以前少了,这是我的更新:

更新了我的urls.py文件,停止使用通用详细信息视图类

url(r'^place/(?P<place_id>\d+)$', views.place_detail, name='detail_place'),

更新了views.py文件上的详细视图方法,以便从两个模型中返回我需要的数据:

def place_detail(request, place_id):
    place = get_object_or_404(Place, pk=place_id)
    placeSubscribers = PlaceSubscriber.objects.all().filter(place=place).values(
         'user__full_name',
         'user__email',
         'user__ionic_id',
         'place__name',
         'date_subscribed',
         'pk'
     )
     return render(request, 'place/place_detail.html', {'place': place, 'placeSubscribers': placeSubscribers}) 

Anda终于更新了我的模板为了从placeSubscribers中提取数据,我可以访问date_subscribed字段

 {% for placeSubscriber in placeSubscribers %}
      <a href="#" class="list-group-item clearfix">
        <span class="pull-left">
          {{ forloop.counter }}. &emsp;
        </span>
        <span class="pull-left">
        <strong>{{ placeSubscriber.user__full_name }}</strong>
         <p>Email: <i>{{ placeSubscriber.user__email }}</i> | Date Subscribed: <i> {{ placeSubscriber.date_subscribed }} </i> </p>
        </span>
        <span class="pull-right">
          <span class="btn btn-xs btn-primary" onclick="sendPushNotificationToUser('{{ placeSubscriber.user__ionic_id }}'); return false;">Send Message</span>
         <span class="btn btn-xs btn-danger" onclick="deletePlaceUser({{ place.id }}, {{ placeSubscriber.pk }}); return false;  ">
           Unsubscribe
           </span>
          </button>
         </span>
         </a>
{% endfor %}