如果我输入重复的值,如何显示消息抛出html页面,但未显示错误消息时抛出html页面

布拉玛迪

如果我发布值保存在数据库中的值,则如果值重复,则显示类似值无法通过渲染html page进行复制的消息。在我的情况下,值存储在数据库中,但如果输入任何重复值,则会引发错误。输入任何重复值时如何显示消息。views.py文件中是否有任何方法method

1062, "Duplicate entry 'django_post' for key 'title'"

这里是models.py代码

 class Post(models.Model):
     title= models.CharField(max_length=100, unique=True)
     content= models.TextField()

view.py文件

def createpost(request):
   if request.method == 'POST':
       if request.POST.get('title') and request.POST.get('content'):
            post = Post()
            post.title = request.POST.get('title')
            post.content = request.POST.get('content')
            post.save()

       return render(request, 'emp.html')

emp.html档案

<html lang="en">
<head>
<title>Create a Post </title>
</head>

<body>
<h1>Create a Post </h1>
<form action=" " method="POST">
{% csrf_token %}
Title: <input type="text" name="title"/><br/>
Content: <br/>
<textarea cols="35" rows="8" name="content">
</textarea><br/>
<input type="submit" value="Post"/>
</form>
</body>

</html>

这是我想通过html页面发布数据后要给html页面的html文件

plicate.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Duplicate values are entered</h1>

</body>
</html>
巴拉特克

您应该更改HTML页面:

替换您的代码:

return render(request, 'emp.html')

至:

return render(request, 'inserted.html')

要么

在里面添加return语句:

 if request.POST.get('title') and request.POST.get('content'):
    ......

    return render(request, 'inserted.html')

models.py

class Post(models.Model):
     title= models.CharField(max_length=100, unique=True)
     content= models.TextField()

当您定义title必须是唯一的时,因此您需要处理重复的异常并在UI端显示错误消息。

view.py

在try除了块之内编写您的保存邮政编码:

try:
    post = Post()
    post.title = request.POST.get('title')
    post.content = request.POST.get('content')
    post.save()
except IntegrityError as e:
    # add your error message

链接将帮助您如何添加和显示错误消息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

错误页面上显示成功消息

在表单页面上显示表单错误消息

我可以在错误消息控件中显示HTML吗?

输入错误时如何显示消息框?

用户输入并显示错误消息时如何验证文本框值?

表单不完整时,JS / HTML我的表单提交按钮未显示错误消息

在角度页面中显示全局错误消息

如果我在图像上显示文本,如何在html页面中呈现图像时使图像上的文本可选?

如果HTML输入错误,如何显示错误消息

如何在我的HTML页面上显示JS值

页面加载时显示错误消息

打开终端时抛出错误消息

如何在管理页面顶部显示我自己的客户消息

如果输入不以字母开头,如何使程序显示错误消息

登录失败后在登录页面中显示错误消息-如果条件不起作用

显示html消息时出现Android Webview错误

在html页面中显示存储在输入数组中的值

重氮-错误显示消息(当您从页面获取内容时)

直接页面时我无法显示验证错误消息

如何使错误消息显示在输入按钮的右侧?

我的消息没有显示在我的显示页面上

Chrome 扩展程序中的沙盒页面通过 iframe 传递消息时抛出错误

在 HTML 页面中单击“保存”后如何显示成功消息

输入负数时显示错误消息。我应该使用 try 语句吗?如何使用?

当我尝试显示错误消息时,PHP 页面未加载

html 元素已加载到我的 html 页面中但未显示 - 需要修复吗?

如果用户输入“0 > toyDiscount >= 100”,我如何显示消息

如果输入值重复,如何显示错误验证?

如果测试失败,如何显示错误消息?