如何创建一个两步表单或向导,其中模型有一个外键,但外键在第二步被选中? [英] How can I create a two step form or wizard, where the model has a foreign key, but the foreign key is selected at the second step?

查看:116
本文介绍了如何创建一个两步表单或向导,其中模型有一个外键,但外键在第二步被选中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户首先将文件上传到服务器,然后下一页用户将填写book_title等书籍的详细信息,如何做?

I want the User upload the file to the server first then next page User will fill the Book details like book_title, etc, how to do this.?

我的模型

class Books(models.Model):
    user = models.ForeignKey(User)
    book_title = models.CharField(max_length = 100)

class Files(models.Model):
    books = models.ForeignKey(Books, null=True, blank=True)
    file = models.FileField(upload_to='uploads/%Y/%m/%d')#content_file_name)

我看到教程他们的工作流程像这样

I saw the tutorial their work flow like this


  1. 第一个用户登录然后创建书名

  2. ,然后在下一个屏幕上,他们将上传文件
    服务器。

  1. First user login then create the book title
  2. and then followed by the next screen they will upload the files to server.

注意:对于这种情况我们有 book_id 所以很容易将文件上传到该book_id

Note: For this case We having book_id so it's easy to upload files to that book_id

但是我希望这个方法是 / code>如

But i want this method to be reverse like


  1. 用户首先将文件上传到服务器

  2. ,然后下一页用户将添加

注意:对于这种情况,我们没有 book_id 首先,但是没有book_id我如何将文件发送到服务器,我知道排除表单中的书籍和 null& ;空白 true将会工作,但后来如何将图书详细信息保存到上传的文件?

Note: For this case we don't have book_id first, But without book_id how can i sent files to server, i know exclude the books in form and null & blank true will work but later how can i save the Book details to uploaded files ??

我正在使用dropzonejs进行拖放上传功能。代码正常工作,每当用户放置文件,它将立即将文件推送到服务器。

I am using dropzonejs for drag and drop upload functionality. The code is working fine, whenever user drop the file then it will push the file to server immediately.

这是我试过的;

if form.is_valid():
            new_file = Files(file = request.FILES['file'])
            request.session['file'] = new_file

但可惜的是,这不起作用,因为文件像 pdf或img 文件不存储在会话中,有没有办法这样做?

but sadly that's not working because files like pdf or img files not stored in session, Is there any way to do this?

推荐答案

如果我把您的问题缩小到其基本格式,我相信你所要求的是:

If I narrow down your question to its basic form, I believe what you are asking is:

我如何创建两步表单或向导,其中模型有外键,但外键在第二步选择?

为了解决这个问题,首先你需要要确保在你的模型中,外键是可选的,通过传递 null = True,blank = True

To solve this problem, first you need to make sure in your model, the foreign key is optional by passing in null=True, blank=True.

接下来,您需要创建表单,代表您的过程的每个步骤:

Next, you need to create your forms, which represent each "step" of your process:

class BookFormStep1(models.ModelForm):
    class Meta:
        model = Files
        fields = ('file',)

class BookFormStep2(models.ModelForm):
    class Meta:
        model = Books
        fields = ('book_title',)

接下来,使用 WizardView mixin

Next, use the WizardView mixin in your views:

class NewBookWizard(SessionWizardView):
    form_list = [BookFormStep1, BookFormStep2]
    template_name = 'add_book_wizard.html'

    def done(self, form_list, **kwargs):

        book = Books()
        data = Files()

        file_form, book_form = form_list

        data.file = file_form.cleaned_data['file']
        data.save()

        book.user = self.request.user
        book.title = book_form.cleaned_data['book_title']
        book.save()

        data.book = book
        data.save()

        return render(request, 'thank-you.html')

您的模板已经为您提供在文档中

Your template is already provided for you in the documentation:

{% extends "base.html" %}
{% load i18n %}

{% block head %}
{{ wizard.form.media }}
{% endblock %}

{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
    {{ wizard.form.management_form }}
    {% for form in wizard.form.forms %}
        {{ form }}
    {% endfor %}
{% else %}
    {{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type="submit" value="{% trans "submit" %}"/>
</form>
{% endblock %}

这篇关于如何创建一个两步表单或向导,其中模型有一个外键,但外键在第二步被选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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