def resource_edit(request, slug): resource = get_object_or_404(Resource, slug__exact=slug) if not resource.is_editable_by(request.user): raise Http404 if request.method == 'POST': form = ResourceForm(request.POST, request.FILES, instance=resource) if form.is_valid(): instance = form.save() if form.cleaned_data.get('author'): instance.author = form.cleaned_data['author'] instance.save() return redirect(instance.get_absolute_url()) else: form = ResourceForm(instance=resource) context = { 'form': form, 'object': resource, } return TemplateResponse(request, 'resources/object_edit.html', context)
def resource_add(request): if request.method == 'POST': form = ResourceForm(request.POST, request.FILES) if form.is_valid(): instance = form.save(commit=False) instance.contact = request.user if form.cleaned_data.get('author'): instance.author = form.cleaned_data['author'] instance.save() form.save_m2m() return redirect(instance.get_absolute_url()) else: form = ResourceForm() context = { 'form': form, } return TemplateResponse(request, 'resources/object_add.html', context)