django - how to implement a 2-step publish mechani

2019-07-10 21:01发布

问题:

I'm new to both web development and django so maybe that's a noob question.

I want to do the following:

  1. Ask user to fill some form and submit it.
  2. Then, parse and format the content and display it back to the user to let him verify it.
  3. User can accept the result or go back to the previous view, update data and resend.

This is as far as I can think:

views.py

def add_content(request):
    if request.method == 'POST':
        form = AddContentForm(request.POST)
        if form.is_valid():
            content = form.save(commit=False)
            return verify_content(request, content)
    else:
        form = AddContentForm()
    return render(request, 'myapp/add_content.html', {'form' : form})

def verify_content(request, content):
    return render(request, 'myapp/verify_content.html', {'content' : content})

The verify_content template will obviously contain two buttons ('back', 'ok'), but I don't know how to pass the content object to a view for saving it in the db, or send it back to the previous view from there. Should I use js? Can i do it with just server side code?

Maybe my whole logic is wrong. Should I save the object in the db before verification and then delete it if needed (sounds ugly)? What is a good way to implement this?

Thanks in advance for your time.

回答1:

You could use the users session for this:

request.session['content'] = content

and in the view where the user should verify his input do:

content = request.session['content']

and voilá you got the content between 2 views.

Django also secures that users can't tinker with its data by either saving it server side, or in a signed cookie.



回答2:

I would save the form with commit=True in the add_content view, and would add a verified field or something to the model. Then you can append the pk as GET parameter to the link which will get you back to add_content view from verify. You can extract the parameter from request.GET dict.