开发者

Want to display the latest note entry only in Django

I have another Django form that stores notes and again it has the user, client and datetime stored automatically. What I want to do this time is create some note form that allows you to add a note for a particular client, and every time you add a note it displays the latest result and only the latest result in the client_details.htmltemplate. So every time add a new note it will keep on replacing the existing note. How can I achieve this?

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开发者_JAVA技巧':client}, context_instance = RequestContext(request))


add to your view:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜