def edit_link(request, space_name, slug_link): try: space = Space.objects.filter(user=request.user).get(slug=space_name) link = get_object_or_404(space.link_set.all(), slug=slug_link) #space.category_set.get(title=slug_category) #category = Category.objects.filter(space__slug=space_name).filter(categories__slug=slug_category) except Space.DoesNotExist: print "No hay links que cuadren." raise Http404 if request.method == 'POST': form = LinkForm(instance=link, data = request.POST) if form.is_valid(): form.save() kwargs = {'space_name': space_name} return HttpResponseRedirect(reverse('space_space_list_objects', kwargs=kwargs)) else: form = LinkForm(instance=link) return render_to_response('space/link_form.html', {'form': form, 'space': Space.objects.filter(user=request.user).get(slug=space_name), 'user': request.user, 'add': False}, context_instance=RequestContext(request))
def add_link(request, space_name): """ This function creates a new link associated with the logged in user **Type:** * private **Arguments:** * request: Request object * space_name: string containing the space_name to which belongs this file **Template:** * ``space/link_form.html`` **Context-variables:** * ``form``: the form itself * ``space``: the corresponding :class:`Space <space.models.Space> object * ``user``: the given username * ``add``: boolean value **Decorators:** :func:`django.contrib.auth.decorators.login_required` """ if request.method == 'POST': form = LinkForm(data = request.POST) try: if form.is_valid(): new_link = form.save(commit=False) new_link.space = Space.objects.filter(user = request.user).get(slug=space_name) new_link.posted_by = request.user new_link.save() kwargs = {'space_name':new_link.space.slug} return HttpResponseRedirect(reverse('space_space_list_objects',kwargs=kwargs)) except DuplicateValuesAreNotUnique: form.non_field_errors = "This link already exists." else: form = LinkForm() return render_to_response('space/link_form.html', {'form': form, 'space': Space.objects.filter(user=request.user).get(slug=space_name), 'user': request.user, 'add': True}, context_instance=RequestContext(request))