django - 如何将处理后的表单数据返回到模板而不保存到数据库中?
问题描述
我正在做一个英语语法检查器。我有两个模型 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})
...
解决方案
推荐阅读
- sapui5 - 两个系统(7.51 和 7.52)之间 OData 的差异
- python - 如何在视图函数上运行 Django APScheduler
- mongodb - 从 docker 容器获取 mongodb 备份时出错
- angular - 在同一个组件文件中调用另一个类的函数
- vue-cli-4 - vue-cli4 用于多页使用 `pages` 配置中的 `indexPath`
- powershell - Windows 更新 API CopyToCache(IStringCollection) - 指定的转换无效
- reactjs - 在 iOS 设备上的 React 应用程序中断应用程序中的正则表达式负向后追溯
- python - 如何使用 Python 或 PowerBI 中的给定数据集制作以下图表
- python - 具有权重和类型相关节点和边的网络图
- php - 试图获取非对象的属性“approval_code”