首页 > 解决方案 > 如何将处理后的表单数据返回到模板而不保存到数据库中?

问题描述

我正在做一个英语语法检查器。我有两个模型 Sentence 和 Error。用户将在表单中输入一个句子,错误(如果有)将生成并以编程方式附加到句子中。现在我有两种类型的用户:免费和付费。我不想将结果保存到免费帐户的数据库中。如何在模板中显示结果而不将它们保存到数据库中?

# noting gets displayed in the template with this code
...
if request.method == 'POST':
        form = SentenceForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            form.save(commit=False)
            sent_str = cd['sent']
            s = Sentence(sent=sent_str, sent_correct=sent_str)
            s.author = user
            # s.save(commit=False) --- This line throws an error
            if user.is_basic or user.is_standard or user.is_premium:
                s.save()
            sent = nlp(sent_str)
            sents = []
            subjs = [t for t in sent if t.dep_ in subj_ls]
            two_subj = False
​
            if len(subjs) >= 2:
                two_subj = True
​
            if not subjs:
                has_imp_patt = imp_patt(sent)
                if has_imp_patt:
                    pass
                else:
                    print('Line 60')
                    msg = '<span style="color:#be0000">Subject\\verb not found</span>'
                    m = Message(msg=msg, sent=s)

                    if user.is_basic or user.is_standard or user.is_premium:
                        m.save()
                    form = SentenceForm()
                    return render(request, 'home.html', {'sentences': sentences, 'form': form})
...

标签: djangodjango-modelsdjango-forms

解决方案


推荐阅读