django - 如何实现两步发布机制 [英] django - how to implement a 2-step publish mechanism

查看:112
本文介绍了django - 如何实现两步发布机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要执行以下操作:


我很喜欢网页开发和django,所以也许这是一个noob的问题。


  1. 请用户填写一些表单并提交。

  2. 然后,解析并格式化内容并将其显示回用户让他验证它。

  3. 用户可以接受结果或返回上一个视图,更新数据并重新发送。

这是我能想到的:



views.py



  def add_content(request):
如果request.method =='POST':
form = AddContentForm(request.POST)
如果form.is_valid() :
content = form.save(commit = False)
返回verify_content(请求,内容)
else:
form = AddContentForm()
return render 'myapp / add_content.html',{'form':form})

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

verify_content模板显然会包含两个按钮('back','ok'),但是我不知道如何将内容对象传递给视图以将其保存在数据库中,或者将其发回到之前的视图。我应该用js吗我可以使用服务器端代码来实现吗?



也许我的整个逻辑是错误的。我应该在验证之前将对象保存在数据库中,然后在需要时删除它(听起来很丑陋)?这是一个很好的方法来实现这个?



提前感谢你的时间。

解决方案/ div>

您可以使用用户会话

  request.session ['content'] = content 

,用户应验证其输入的视图:

  content = request.session ['content'] 

和voilá您获得了两个视图之间的内容。



Django还确保用户无法通过保存服务器端或签名的Cookie来修复其数据。 p>

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.

解决方案

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.

这篇关于django - 如何实现两步发布机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆