Python中文
首页
教程
问答
标签
搜索
登录
注册
Django1.8若表单条目查询结果和数据库不匹配,则在同一页面上显示警告消息,而不是“None”或引发异常pag
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我很感激下面的答案,但很抱歉我仍然没有解决这个问题,也许我没有正确理解它们。因此,为了得到更清楚的答案,我悬赏了一下。在</p> <p>用户在表单中输入一些信息后,这些信息作为一个查询来过滤数据库得到结果,如果数据库中没有相应的记录,我怎么可能在当前页面或重定向页面上显示一个警告,提醒用户“没有对应的数据”。在</p> <p><a href="https://i.stack.imgur.com/fDPrv.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/fDPrv.jpg" alt="enter image description here"/></a></p> <p>举例说明:如果用户输入“EU”和“India”,数据库中肯定没有对应的记录。表单允许用户将字段留空。在</p> <p>我以前用的是raisevalidationerror,如果查询结果和数据库不匹配,它将转到一个黄色的“异常”页面,这是不友好的。我想在提交后立即在同一表单页面上显示错误消息:</p> <p><strong>视图.py</strong></p> <pre><code>from django.contrib import messages class InputFormView(FormView): template_name = 'entryform.html' form_class = EntryForm def get_success_url(self): params = { 'department': self.request.POST.get('company'), 'person': self.request.POST.get('region') } return ''.join([reverse('final'), '?', urllib.urlencode(params.items())]) class FinalView(ListView): context_object_name = 'XXX' template_name = 'XXX.html' model = Final def get_queryset(self): form = InputForm(self.request.GET) if form.is_valid(): department = form.cleaned_data['department'] person = form.cleaned_data['person'] if department !="" and person !="": if Final.objects.filter(department=department,person=person).exists(): queryset=Final.objects.filter(department=department,person=person) return queryset else: msg="no corresponding data exists!" form.add_error('department', msg) form.add_error('person', msg) elif department =="" and person !="": if Final.objects.filter(person=person).exists(): queryset=Final.objects.filter(person=person) return queryset else: msg="no corresponding data exists!" form.add_error('department', msg) form.add_error('person', msg) elif ........ else: #if form not valid messages.error(request, "Error") def get_context_data(self,**kwargs): query_set = self.get_queryset() if query_set is not None: context["sales"] = self.get_queryset().aggregate(Sum('sales')) </code></pre> <p><strong>html</strong></p> ^{pr2}$ <p>如果我不使用ValidationError方法,它将重定向到结果页,将所有内容显示为“None”。但我想显示一条警告信息。我在网上看到了一个ajax示例,有点复杂。有没有更容易实现的方法</p> <p>提前谢谢。在</p> <p>谢谢。在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>所有这些逻辑都属于形式本身。如果您将它放在<code>clean</code>方法中,那么验证错误将被现有的Django逻辑捕获,并且您可以使用<code>{{ form.non_field_errors }}</code>在模板中显示该错误。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
尝试加密和解密文本文件(Python)
9 回答
尝试加密文本文件会导致类型错误
10 回答
尝试加密时溢出错误
7 回答
尝试加载.plist文件时出现“无此类文件或目录”错误
7 回答
尝试加载“blog.templatetags.blog_标记”时引发ImportError:没有名为“markdown”的模块
7 回答
尝试加载“menus.templatetags.menus\u标记”时引发ImportError:无法从“menus.models”导入名称“menus”
5 回答
尝试加载5GB文本fi时发生Python内存错误
2 回答
尝试加载5GB文本文件时出现MemoryError
10 回答
尝试加载Django模板时出现渲染错误
10 回答
尝试加载exi时出错
3 回答
尝试加载imag时使用精灵表失败
3 回答
尝试加载json.Python时出错
1 回答
尝试加载json文件时遇到此错误:json.decoder.jsondeCoderror:预期值:第1行第1列(char 0)
7 回答
尝试加载keras和tensorflow,我仍然得到ModuleNotFoundError:在我的Anaconda环境中没有名为'tensorflow_core.estimator'的模块
9 回答
尝试加载Librosa的示例fi时出现“找不到文件”错误
5 回答
尝试加载matplotlib时发生iPython笔记本错误
4 回答
尝试加载MP3时出现PyGame错误
1 回答
尝试加载pickled matplotlib figu时发生AttributeError
2 回答
尝试加载pickle文件时出错
1 回答
尝试加载Pytoch模型时遇到问题:“在模块中找不到标识”
4 回答