<bdo id='kvsjh'></bdo><ul id='kvsjh'></ul>

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

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

      1. 允许"-&quot;Django 管理界面中用户名中的字符

        Allowing the quot; - quot; character in usernames in the Django Admin interface(允许-quot;Django 管理界面中用户名中的字符)

          <tbody id='Cdpqw'></tbody>

        <tfoot id='Cdpqw'></tfoot>
      2. <legend id='Cdpqw'><style id='Cdpqw'><dir id='Cdpqw'><q id='Cdpqw'></q></dir></style></legend>
          <bdo id='Cdpqw'></bdo><ul id='Cdpqw'></ul>
              • <small id='Cdpqw'></small><noframes id='Cdpqw'>

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

                  本文介绍了允许"-&quot;Django 管理界面中用户名中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我们的 web 应用中,我们需要在用户名中允许使用破折号-".我已经为消费者注册过程启用了这个正则表达式r'^[w-]+$'

                  In our webapp we needed to allow dashes "-" in our usernames. I've enabled that for the consumer signup process just fine with this regex r'^[w-]+$'

                  如何告诉管理应用程序以便我可以在 auth > users 中编辑用户名以允许用户名中的-"字符?目前我无法编辑任何带有破折号的用户名,因为它会在用户名上返回验证错误.

                  How can I tell the admin app so that I can edit usernames in auth > users to allows the "-" character in usernames? Currently I am unable to edit any usernames with dashes in them as it will return a validation error on the username.

                  如果可能,我想尽量避免直接修补 django.我对编程还很陌生,但这就是我要使用子类化"的目的吗?

                  I'd like to try and avoid patching django directly if possible. I'm fairly new to programming but is this what I would use "subclassing" for?

                  推荐答案

                  这应该像覆盖 User ModelAdmin 类的行为一样简单.在您的一个应用程序中,在 admin.py 中包含以下代码.

                  This should be as simple as overriding the behavior of the User ModelAdmin class. In one of your apps, in admin.py include the following code.

                  from django.contrib import admin
                  from django import forms
                  from django.contrib.auth.models import User
                  from django.contrib.auth.admin import UserAdmin
                  from django.contrib.auth.forms import UserCreationForm, UserChangeForm
                  
                  class MyUserCreationForm(UserCreationForm):
                      username = forms.RegexField(
                          label='Username', 
                          max_length=30, 
                          regex=r'^[w-]+$',
                          help_text = 'Required. 30 characters or fewer. Alphanumeric characters only (letters, digits, hyphens and underscores).',
                          error_message = 'This value must contain only letters, numbers, hyphens and underscores.')
                  
                  class MyUserChangeForm(UserChangeForm):
                      username = forms.RegexField(
                          label='Username', 
                          max_length=30, 
                          regex=r'^[w-]+$',
                          help_text = 'Required. 30 characters or fewer. Alphanumeric characters only (letters, digits, hyphens and underscores).',
                          error_message = 'This value must contain only letters, numbers, hyphens and underscores.')
                  
                  class MyUserAdmin(UserAdmin):
                      form = MyUserChangeForm
                      add_form = MyUserCreationForm
                  
                  admin.site.unregister(User)
                  admin.site.register(User, MyUserAdmin)
                  

                  这里有一点解释.

                  第一个类定义 (MyUserCreationForm) 是 UserCreationForm 的子类(是的,您的术语是正确的).这是在 Django 管理站点中单击添加用户"时出现的表单.我们在这里所做的只是重新定义 username 字段以使用我们改进的、接受连字符的正则表达式,并更改 helptext 以反映这一点.

                  The first class definition (MyUserCreationForm) is a subclass (yes your terminology is correct) of the UserCreationForm. This is the form that appears when you click "Add User" in the Django Admin site. All we are doing here is redefining the username field to use our improved, hyphen-accepting regex, and changing the helptext to reflect this.

                  第二个类的定义是一样的,除了 UserChangeForm.

                  The second class definition does the same, except for the UserChangeForm.

                  最终的类定义是UserAdmin的子类,也就是User模型默认使用的ModelAdmin.在这里,我们声明我们希望在 ModelAdmin 中使用我们新的自定义表单.

                  The final class definition is a subclass of UserAdmin, which is the ModelAdmin that the User model uses by default. Here we state that we want to use our new custom forms in the ModelAdmin.

                  请注意,对于这些子类中的每一个,我们只更改我们必须更改的内容.该类的其余部分将继承自其父级(分别为 UserCreationForm、UserChangeForm 和 UserAdmin).

                  Note that for each of these subclasses, we only change what we have to. The rest of the class will be inherited from its parent (UserCreationForm, UserChangeForm and UserAdmin respectively).

                  最后,我们执行向管理站点注册用户模型的重要步骤.为此,我们取消注册默认的 UserAdmin,然后使用我们改进的 MyUserAdmin 类进行注册.

                  Finally, we perform the important step of registering the User model with the admin site. To do that we unregister the default UserAdmin, and then register with our improved MyUserAdmin class.

                  您会发现使用这些技术可以很容易地自定义 Django 管理站点,尤其是考虑到管理站点只是一个普通的 Django 应用程序.

                  You'll find that the Django admin site is very easy to customize using these techniques, especially considering the admin site is just a regular Django app.

                  这篇关于允许"-&quot;Django 管理界面中用户名中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                  • <tfoot id='z2mzd'></tfoot>
                      <tbody id='z2mzd'></tbody>
                  • <i id='z2mzd'><tr id='z2mzd'><dt id='z2mzd'><q id='z2mzd'><span id='z2mzd'><b id='z2mzd'><form id='z2mzd'><ins id='z2mzd'></ins><ul id='z2mzd'></ul><sub id='z2mzd'></sub></form><legend id='z2mzd'></legend><bdo id='z2mzd'><pre id='z2mzd'><center id='z2mzd'></center></pre></bdo></b><th id='z2mzd'></th></span></q></dt></tr></i><div id='z2mzd'><tfoot id='z2mzd'></tfoot><dl id='z2mzd'><fieldset id='z2mzd'></fieldset></dl></div>

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

                          <bdo id='z2mzd'></bdo><ul id='z2mzd'></ul>
                        • <legend id='z2mzd'><style id='z2mzd'><dir id='z2mzd'><q id='z2mzd'></q></dir></style></legend>