Пример #1
0
def edit_resource(request, community, id):
    """
    This view edits an existing resource / entry.
    
    **Template:**
    ``community/resource_form.html``
    
    **Decorators:**
        :func:`django.contrib.auth.decorators.login_required`
        
    **Context-variables:**
        * form: the form itself
        * add: boolean value that tells the template whether to show ``add`` or ``edit``
    
    """
    resource = get_object_or_404(Resource, author = request.user,
                                           community__slug = community,
                                           id = id)
    if request.method == 'POST':
        form = ResourceForm(instance = resource, data = request.POST)
        if form.is_valid():
            form.save()
            kwargs = {'community': community}
            return HttpResponseRedirect(reverse('community_community_admin_index', kwargs=kwargs))
    else:
        form = ResourceForm(instance=resource)
    return render_to_response('community/resource_form.html',
                              {'form': form,
                               'community': community,
                               'add':False},
                               context_instance=RequestContext(request))
Пример #2
0
def create_resource(request, community):
    """
    This view creates a new resource / entry.
    
    **Template:**
    ``community/resource_form.html``
    
    **Decorators:**
        :func:`django.contrib.auth.decorators.login_required`
        
    **Context-variables:**
        * form: the form itself
        * add: boolean value that tells the template whether to show ``add`` or ``edit``
    
    """
    if request.method == 'POST':
        form = ResourceForm(data = request.POST)
        if form.is_valid():
            resource = form.save(commit=False)
            resource.author = request.user
            resource.community = Community.objects.get(slug=community)
            resource.save()
            kwargs = {'community': community}
            return HttpResponseRedirect(reverse('community_community_admin_index', kwargs=kwargs))
    else:
        form = ResourceForm()
    return render_to_response('community/resource_form.html',
                              {'form': form,
                               'community': community,
                               'add': True},
                               context_instance=RequestContext(request))