1. <i id='Lm42G'><tr id='Lm42G'><dt id='Lm42G'><q id='Lm42G'><span id='Lm42G'><b id='Lm42G'><form id='Lm42G'><ins id='Lm42G'></ins><ul id='Lm42G'></ul><sub id='Lm42G'></sub></form><legend id='Lm42G'></legend><bdo id='Lm42G'><pre id='Lm42G'><center id='Lm42G'></center></pre></bdo></b><th id='Lm42G'></th></span></q></dt></tr></i><div id='Lm42G'><tfoot id='Lm42G'></tfoot><dl id='Lm42G'><fieldset id='Lm42G'></fieldset></dl></div>

      • <bdo id='Lm42G'></bdo><ul id='Lm42G'></ul>
      <tfoot id='Lm42G'></tfoot>

      <small id='Lm42G'></small><noframes id='Lm42G'>

      <legend id='Lm42G'><style id='Lm42G'><dir id='Lm42G'><q id='Lm42G'></q></dir></style></legend>

      1. Django 管理员,自定义错误消息?

        时间:2023-11-07

        <small id='5AyfA'></small><noframes id='5AyfA'>

          <tbody id='5AyfA'></tbody>
        • <tfoot id='5AyfA'></tfoot>
            • <i id='5AyfA'><tr id='5AyfA'><dt id='5AyfA'><q id='5AyfA'><span id='5AyfA'><b id='5AyfA'><form id='5AyfA'><ins id='5AyfA'></ins><ul id='5AyfA'></ul><sub id='5AyfA'></sub></form><legend id='5AyfA'></legend><bdo id='5AyfA'><pre id='5AyfA'><center id='5AyfA'></center></pre></bdo></b><th id='5AyfA'></th></span></q></dt></tr></i><div id='5AyfA'><tfoot id='5AyfA'></tfoot><dl id='5AyfA'><fieldset id='5AyfA'></fieldset></dl></div>

                <legend id='5AyfA'><style id='5AyfA'><dir id='5AyfA'><q id='5AyfA'></q></dir></style></legend>
                  <bdo id='5AyfA'></bdo><ul id='5AyfA'></ul>
                  本文介绍了Django 管理员,自定义错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想知道如何在 Django admin 中显示错误消息.

                  I would like to know how to show an error message in the Django admin.

                  我的网站上有一个私人用户部分,用户可以在其中使用点"创建请求.一个请求从用户的帐户中获得 1 或 2 分(取决于两种类型的请求),所以如果帐户有 0 分,则用户不能提出任何请求......在私人用户部分,这一切都很好,但是用户也可以致电公司并通过电话提出请求,在这种情况下,我需要管理员在用户积分为 0 的情况下显示自定义错误消息.

                  I have a private user section on my site where the user can create requests using "points". A request takes 1 or 2 points from the user's account (depending on the two type of request), so if the account has 0 points the user cant make any requests... in the private user section all this is fine, but the user can also call the company and make a request by phone, and in this case I need the admin to show a custom error message in the case of the user points being 0.

                  任何帮助都会很好:)

                  谢谢大家

                  推荐答案

                  一种方法是覆盖管理页面的 ModelForm.这允许您编写自定义验证方法并非常干净地返回您选择的错误.像这样在 admin.py 中:

                  One way to do that is by overriding the ModelForm for the admin page. That allows you to write custom validation methods and return errors of your choosing very cleanly. Like this in admin.py:

                  from django.contrib import admin
                  from models import *
                  from django import forms
                  
                  class MyForm(forms.ModelForm):
                      class Meta:
                          model = MyModel
                      def clean_points(self):
                          points = self.cleaned_data['points']
                          if points.isdigit() and points < 1:
                              raise forms.ValidationError("You have no points!")
                          return points
                  
                  class MyModelAdmin(admin.ModelAdmin):
                      form = MyForm
                  
                  admin.site.register(MyModel, MyModelAdmin)
                  

                  希望有帮助!

                  这篇关于Django 管理员,自定义错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:django admin 的有效用例? 下一篇:Django 管理员.在更改选择字段上隐藏字段

                  相关文章

                • <small id='yAH4q'></small><noframes id='yAH4q'>

                  <tfoot id='yAH4q'></tfoot>
                    <legend id='yAH4q'><style id='yAH4q'><dir id='yAH4q'><q id='yAH4q'></q></dir></style></legend>
                      <bdo id='yAH4q'></bdo><ul id='yAH4q'></ul>

                      <i id='yAH4q'><tr id='yAH4q'><dt id='yAH4q'><q id='yAH4q'><span id='yAH4q'><b id='yAH4q'><form id='yAH4q'><ins id='yAH4q'></ins><ul id='yAH4q'></ul><sub id='yAH4q'></sub></form><legend id='yAH4q'></legend><bdo id='yAH4q'><pre id='yAH4q'><center id='yAH4q'></center></pre></bdo></b><th id='yAH4q'></th></span></q></dt></tr></i><div id='yAH4q'><tfoot id='yAH4q'></tfoot><dl id='yAH4q'><fieldset id='yAH4q'></fieldset></dl></div>