简体   繁体   中英

How do I save post data using a decorator in Django

I have the following view in my django app.

def edit(request, collection_id):
    collection = get_object_or_404(Collection, pk=collection_id)
    form = CollectionForm(instance=collection)
    if request.method == 'POST':
        if 'comicrequest' in request.POST:
            c = SubmissionLog(name=request.POST['newtitle'], sub_date=datetime.now())
            c.save()
        else:
            form = CollectionForm(request.POST, instance=collection)
            if form.is_valid():
                update_collection = form.save()
                return redirect('viewer:viewer', collection_id=update_collection.id)

    return render(request, 'viewer/edit.html', {'form': form})

It displays a form that allows you to edit a collection of images. The footer of my html contains a form that allows you to request a new image source from the admin. It submits to a different data model than the CollectionForm. Since this is in the footer of every view, I want to extract lines 5-7 of the code and turn it into a decorator. Is this possible and if so how might I go about doing that?

I would make a new view to handle the post of the form. And then stick a blank form instance in a context processor or something, so you can print it out on every page.

If you do want to make a decorator, i would suggest using class based views. That way, you could easily make a base view class that handles the form, and every other view could extend that.

EDIT:

Here's the docs on class based views: https://docs.djangoproject.com/en/dev/topics/class-based-views/intro/

Note, I would still recommend having a separate view for the form POST, but here's what your solution might look like with class based views:

class SubmissionLogFormMixin(object):

    def get_context_data(self, **kwargs):
        context = super(SubmissionLogFormMixin, self).get_context_data(**kwargs)

        # since there could be another form on the page, you need a unique prefix
        context['footer_form'] = SubmissionLogForm(self.request.POST or None, prefix='footer_')
        return context

    def post(self, request, *args, **kwargs):
        footer_form = SubmissionLogForm(request.POST, prefix='footer_')
        if footer_form.is_valid():
            c = footer_form.save(commit=False)
            c.sub_date=datetime.now()
            c.save()

        return super(SubmissionLogFormMixin, self).post(request, *args, **kwargs)


class EditView(SubmissionLogFormMixin, UpdateView):
    form_class = CollectionForm
    model = Collection


# you can use SubmissionLogFormMixin on any other view as well.

Note, that was very rough. Not sure if it will work perfectly. But that should give you an idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM