Пример #1
0
def new_contact(request):
    # Checking for POST request and validation the form
    if request.POST:        
        contactsForm = ContactsForm(request.POST)        
        if contactsForm.is_valid():
            # saving the contact to the DB
            contactsForm.save()
            return render_to_response('add_contact.html', {'added': True, 'first_name': request.POST['first_name'], 'last_name': request.POST['last_name']}, context_instance=RequestContext(request))    
    else:        
        contactsForm = ContactsForm()          
    
    return render_to_response('add_contact.html', {'contactsForm': ContactsForm, 'added': False}, context_instance=RequestContext(request))
Пример #2
0
def edit_contact(request, contact_id):
    # Valid contact since is part of the URL
    # TODO: needs validation in order to avoid directly manipulation
    # of the URL through the browser or a program
    form = Contacts.objects.get(pk=contact_id)
    if request.POST:        
        contactsForm = ContactsForm(request.POST, instance=form)
        if contactsForm.is_valid():            
            contactsForm.save()
            return render_to_response('edit_contact.html', {'added': True, 'first_name': request.POST['first_name'], 'last_name': request.POST['last_name']})
    
    else:
        # Passing the form to the ContacsForm class in order
        # to populate the fields of thee web form
        contactsForm = ContactsForm(instance=form) 
        
    return render_to_response('edit_contact.html', {'contactsForm': contactsForm, 'added': False, 'first_name': form.first_name, 'last_name': form.last_name}, context_instance=RequestContext(request))