示例#1
0
def edit_space(request, id):
    space = get_object_or_404(Space, user=request.user, id=id)
    if request.method == 'POST':
        form = SpaceForm(instance=space, data = request.POST)
        try:
            if form.is_valid():
                new_space = form.save(commit=False)
                space_exist = Space.objects.filter(user=new_space.user,
                                                   title=new_space.title)
                if space_exist:
                    raise DuplicateValuesAreNotUnique
                new_space.save()
                return HttpResponseRedirect(reverse('space_space_list_spaces'))
        except DuplicateValuesAreNotUnique:
            form.non_field_errors = "The title of this space already exist. Please choose another one."
    else:
        form = SpaceForm(instance=space)
    return render_to_response('space/space_form.html',
                              {'form':form,
                               'add':False},
                               context_instance=RequestContext(request))
示例#2
0
def create_space(request):
    if request.method == 'POST':
        form = SpaceForm(data = request.POST)
        try:
            if form.is_valid():
                new_space = form.save(commit=False)
                #print new_space.slug
                new_space.user = request.user
                #new_space.slug = slugify(new_space.title)
                space_exist = Space.objects.filter(user=new_space.user).filter(title=new_space.title)
                if space_exist:
                    raise DuplicateValuesAreNotUnique
                new_space.save()
                return HttpResponseRedirect(reverse('space_space_list_spaces'))
        except DuplicateValuesAreNotUnique:
            form.non_field_errors = "The title of this space already exist. Please choose another one."
    else:
        form = SpaceForm()
    return render_to_response('space/space_form.html',
                              {'form': form,
                               'add':True},
                              context_instance=RequestContext(request))