Django-跨视图保存进度 [英] Django - Form across multiple views with progress saving

查看:84
本文介绍了Django-跨视图保存进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Django项目,为了使表单体验更加流畅,我想将ModelForm分布在几页上。如果登录的用户可以在不实际发布内容的情况下将其进度保存在表单中(在这种情况下,是 JobApplication ,用户可以重新填写该表单)信息,而无需实际发送应用程序。)

I'm working on a Django project and to make the forms experience far smoother I want to spread a ModelForm across a few pages. It would be ideal if users who are logged in can save their progress in the form without actually posting the content (in this case, a JobApplication where users can come back to filling in info without actually sending off the application).

目前,我已经在SO上找到了其他答案,例如;但这仅向我展示了如何利用缓存在存在表单的视图之间存储信息。

Currently I have looked at other answers on SO such as this one; but this only shows me how to utilise caching to store information between the views where the form is present.

Models.py(为了便于阅读,简化了模型,表单和视图):

Models.py (models, forms and views have been simplified for readability):

class JobApplication(models.Model):
    job = models.ForeignKey(JobPost,on_delete=models.SET_NULL,...)
    user = models.ForeignKey(AUTH_USER_MODEL,...)
    details = models.CharField(max_length=300)
    skills = models.CharField(max_length=300)
    feedback = models.CharField(max_length=300)
    #... [insert more fields] ...

Forms.py:

class Application(forms.ModelForm):
    details = forms.CharField() # To go in page 1 of the form process
    skills = forms.CharField() # To go in page 2
    feedback = forms.CharField() # To go in page 3
    class Meta:
        model = JobApplication
        fields = ['details','skills','feedback']

Views.py:

from . import forms
def view1(request):
    form = forms.Application()
    if request.method == 'POST':
        form = forms.Application(data=request.POST)
        ... some logic here which I am not sure of ...
    return render(request, 'view1.html', {})

def view2(request):
    form = forms.Application()
    if request.method == 'POST':
        form = forms.Application(data=request.POST)
        ...
    return render(request, 'view2.html', {})

def view3(request):
    form = forms.Application()
    if request.method == 'POST':
        form = forms.Application(data=request.POST)
        ...
    return render(request, 'view3.html', {})

请注意,我很高兴编辑表单或模型以实现此多页面功能,并保存进度

Note that I'm happy to edit my forms or models to achieve this multi-page, save progress effect that you may see on job sites.

让我知道是否可以添加更多的代码有用,因为我不愿意

Let me know if there's any more code I can add that will be useful, since I'm not too sure what else will be required.

谢谢!

推荐答案


  1. 我在我的应用程序中有一个类似的用例,我所做的是在模型中创建了
    多个表单,并在中央视图中控制了
    表单的进度。

  1. I had a similar use case in my application what I did was created multiple forms out the models and a central view controlling the progress of the form.

该视图具有必须传播的表单列表

The view has a list of forms it has to propagate through

GET : /form/<index> => form/0
POST : Save data to the form


  • 表单最初没有初始数据,对于索引> 0,
    初始数据将是先前保存的模型对象

  • Initially the form will have no initial data, for index > 0 the initial data will be the previously saved model object

    当用户单击下一个增量时,URL索引计数器,先减少
    ,不要跳过任何内容

    When user clicks on next increment the URL index counter, Decrease it for prev, Don't save anything on skip

    以下是要点看起来如何。
    https://gist.github.com/bhavaniravi/b784c57ae9d24fce359ae44e2e90b8e3 >

    Here is a gist of how it would look. https://gist.github.com/bhavaniravi/b784c57ae9d24fce359ae44e2e90b8e3


    我不知道这是否是有史以来最佳的优化方法,但这就是我所做的。任何有关改进的建议都受到欢迎

    I don't know if this is the best optimized method of all times but this is what I did. Any suggestions on improvement is most welcomed

    这篇关于Django-跨视图保存进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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