【问题标题】:Want to display the latest note entry only in Django只想在 Django 中显示最新的笔记条目
【发布时间】:2011-06-27 14:22:20
【问题描述】:

我有另一个存储笔记的 Django 表单,它再次自动存储了用户、客户端和日期时间。这次我想做的是创建一些注释表单,允许您为特定客户添加注释,每次添加注释时,它都会显示最新结果,并且仅显示最新结果client_details.html模板。因此,每次添加新便笺时,它都会继续替换现有便笺。我怎样才能做到这一点?

forms.py

class NoteForm(forms.ModelForm):
    class Meta:
        model = models.Note
        exclude = ('client','user','datetime')

Views.py

@login_required
def get_client(request, client_id = 0):
    client = None
    try:
        client = models.Client.objects.get(pk = client_id)
        notes = client.note_set.all()
        comments = client.comment_set.order_by('-datetime')
    except:
        pass
    return render_to_response('client_details.html', {'client':client, 'notes':notes, 'comments':comments}, context_instance = RequestContext(request))

@login_required
    def add_notes(request, client_id = 0):
        client = None
        contact = None
        try:
            client = models.Client.objects.get(pk = client_id)
        except:
            return HttpResponseNotFound()

        if request.method == 'POST':
            form = forms.NoteForm(request.POST)
            if form.is_valid():
                note = form.save(commit=False)
                note.user = request.user
                note.client = client
                note.save(True)
                request.user.message_set.create(message = "Note is successfully added.")
                return HttpResponse("<script language=\"javascript\" type=\"text/javascript\">window.opener.location = window.opener.location; window.close();</script>")
        else:
            form = forms.NoteForm()
        return render_to_response('note_form.html', {'form':form, 'client':client}, context_instance = RequestContext(request))

【问题讨论】:

    标签: python django views


    【解决方案1】:

    添加到您的视图中:

    try:
        last_note = client.note_set.latest("datetime")
    except DoesNotExist:
        last_note = None
    

    【讨论】:

      猜你喜欢
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多