Django下拉列表将选择的值传递给查看

时间:2016-02-10 20:41:48

标签: django history dropdown

我在django中创建了一个应用程序,允许我编辑对象的值并跟踪编辑的历史记录。

我想在django做如下:

从下拉列表中选择一个值,并仅显示在当前编号与所选值

匹配的对象

e.g。

对象编辑于01-02-2016,02-02-2016,03-02-2016

所以下拉列表有值:01,02,03 我选择02并显示在02上编辑的对象。

到目前为止,我知道很热,可以创建一个列表,其中包含编辑对象的日期。我知道如何通过选定的开始和结束范围过滤对象。

目前我手动设置开始和结束范围,但我想按照上面的说明自动化它。

请问任何想法?

视图

class AllRoomsView(ListView):
    template_name = 'prostats/roomsdetail.html'
    queryset = Room.objects.all()

    def get_context_data(self, **kwargs):
        context = super(AllRoomsView, self).get_context_data(**kwargs)

        # get context
        context['rooms'] = Room.objects.all()
        context['rlog'] = RoomLog.objects.all()

        # get day only
        roomsdates = []
        for r in context['rlog']:
            roomsdates.append(r.update_date.day)

        # get update days values
        roomextracted = []
        for i in roomsdates:
            if i not in roomextracted:
                roomextracted.append(i)

        # get context again
        context['roomextracted'] = roomextracted

        choosestart = '2016-02-02'
        choosend = '2016-02-04'
        context['roomfiltersettime'] = RoomLog.objects.filter(update_date__gt = choosestart,
                                                                  update_date__lte = choosend)

        return context

模型

class Room(models.Model):
    room_name = models.CharField(max_length= 10)
    room_value = models.PositiveSmallIntegerField(default=0)
    flat = models.ForeignKey(Flat)
    created_date = models.DateField(auto_now_add= True)
    created_time = models.TimeField(auto_now_add= True)

    def __init__(self, *args, **kwargs):
        super(Room, self).__init__(*args, **kwargs)
        self.value_original = self.room_value


    def save(self, **kwargs):
        with transaction.atomic():
            response = super(Room, self).save(**kwargs)
            if self.value_original != self.room_value:
                room_log = RoomLog()
                room_log.room = self
                room_log.room_value = self.value_original
                room_log.save()

            return response

    class Meta:
        ordering = ('room_name',)

    def __unicode__(self):
        return self.room_name


class RoomLog(models.Model):
    room = models.ForeignKey(Room)
    room_value = models.PositiveSmallIntegerField(default=0)
    update_date = models.DateField(auto_now_add= True)
    update_time = models.TimeField(auto_now_add= True)

    def __str__(self):
        return '%s | %s | %s' % (self.room, self.update_date, self.update_time)
修改

我确实设法创建下拉列表,但不知道如何将其发布到另一个视图。

所以在我的主视图中我有这个:

context['rlog'] = RoomLog.objects.all()
        roomsdates = []
        for r in context['rlog']:
            if r not in roomsdates:
                roomsdates.append(r.update_date)
        # get update days values
        roomextracted = []
        for i in roomsdates:
            if i not in roomextracted:
                roomextracted.append(i)
        context['roomextracted'] = roomextracted

这给了我下拉列表。我在我的模板中编写了下拉列表,如下所示:

<select name="dropdown">
    <option selected="selected" disabled>Dates</option>
    {%  for i in roomextracted %}
    <option value=i>{{ i }}</option>
    {% endfor %}
  </select>

我不知道如何将所选值POST到另一个我有GET的视图,如下所示

class AllRoomsView(ListView):
    template_name = 'prostats/roomsdetail.html'
    queryset = Room.objects.all()

    def get_context_data(self, request, **kwargs):
        context = super(AllRoomsView, self).get_context_data(**kwargs)
        # not relevant code skipped here 

        answer = request.GET['dropdown']
        choosend = answer
        context['roomfiltersettime'] = RoomLog.objects.filter(update_date__lte = choosend)

0 个答案:

没有答案