Beispiel #1
0
def write(request):
    """View to use as the guestbook writing page.
    
    Presents a form to write an entry into the guestbook. Upon submitting, the
    form values are validated and saved to the database, representing a
    digital signature.
    
    If form validation fails, the form is shown again along with an error
    message.
    
    A Django HTTP Request object must be passed. An HTTP Response is returned.
    """
    guestbook_error = None
    if request.method == 'POST':
        try:
            new_entry = Entry(
                ip=request.META['REMOTE_ADDR'],
                namefirst=request.POST['namefirst'],
                namelast=request.POST['namelast'],
                comment=request.POST['comment']
                )
            new_entry.full_clean()
            new_entry.save(force_insert=True)
        except ValidationError as e:
            guestbook_error = '<br /><br />'.join(e.messages)
        else:
            return redirect(reverse('guestbook:index'))
    context = {'guestbook_error': guestbook_error}
    return render(request, 'guestbook/write.html', context)