如何将已处理的表单数据返回到模板而不将其保存到数据库?

时间:2019-08-14 12:12:16

标签: django django-models django-forms

我正在做一个英语语法检查器。我有两个模型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})
...

0 个答案:

没有答案