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

      1. <small id='fOaG8'></small><noframes id='fOaG8'>

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

        如何通过电子邮件强制执行 Django 用户注册单步(而不是两步)过程?

        How can I have Django user registration single step (instead of two step)process with email compulsory?(如何通过电子邮件强制执行 Django 用户注册单步(而不是两步)过程?)
      4. <i id='qwLjL'><tr id='qwLjL'><dt id='qwLjL'><q id='qwLjL'><span id='qwLjL'><b id='qwLjL'><form id='qwLjL'><ins id='qwLjL'></ins><ul id='qwLjL'></ul><sub id='qwLjL'></sub></form><legend id='qwLjL'></legend><bdo id='qwLjL'><pre id='qwLjL'><center id='qwLjL'></center></pre></bdo></b><th id='qwLjL'></th></span></q></dt></tr></i><div id='qwLjL'><tfoot id='qwLjL'></tfoot><dl id='qwLjL'><fieldset id='qwLjL'></fieldset></dl></div>
          <tbody id='qwLjL'></tbody>

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

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

            • <tfoot id='qwLjL'></tfoot>
              1. <small id='qwLjL'></small><noframes id='qwLjL'>

                  本文介绍了如何通过电子邮件强制执行 Django 用户注册单步(而不是两步)过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  一旦管理员将新用户添加到管理站点,我希望 Django 向用户电子邮件地址发送一封包含登录详细信息的电子邮件.所以我尝试使用 Django 信号,但只是因为 django 用户注册是一个两步过程,信号会得到通知仅在第一步中并在没有电子邮件地址的情况下调用电子邮件功能(在第二步中出现).我的信号码:

                  I want Django to send an email to user email-address with Login details once admin adds a new user to admin site.So I tried using Django signals for that but just becoz django user registration is a two step process signals get notified in first step only and called email function without email address(which comes in second step). My signal code:

                  def email_new_user(sender, **kwargs):
                      if kwargs["created"]:  # only for new users
                          new_user = kwargs["instance"]
                         send_mail('Subject here', 'Here is the message.', 'from@example.com',['to@example.com'], fail_silently=False)
                  
                  
                  post_save.connect(email_new_user, sender=User)
                  

                  所以我试图克服这个问题.我在 admin.py 中使用此代码

                  So what i tried to overcome this problem.I use this code in admin.py

                  class UserAdmin(admin.ModelAdmin):
                      list_display = ('username', 'email', 'first_name', 'last_name', 'date_joined', 'last_login')
                      search_fields = ['username', 'email']
                      filter_horizontal = ('user_permissions',)
                  
                  admin.site.unregister(User)
                  admin.site.register(User, UserAdmin)
                  

                  这使得所有注册过程成为一个单一的步骤过程,我的信号开始工作并在添加新用户时向 user_id 发送邮件.但问题出现在此之后:

                  This makes all registration process a single step process and my signals start working and sending mail to user_id on new user addition.But the problem came after this were:

                  1.用户密码没有转换成hash,在进入表单时可见,导致用户无法登录管理站点.

                  2.表单中的电子邮件字段不是强制性的,我希望是强制性的.

                  请帮帮我:(

                  I tried your code But I m still at same place where i was before posting this question.
                  the code i used in my admin.py is:
                  from django.contrib import admin
                  from mysite.naturefarms.models import *
                  from django.contrib.auth.models import User,Group
                  from django.contrib.auth.admin import UserAdmin
                  from django.contrib.auth.forms import UserCreationForm, UserChangeForm
                  from django import forms
                  from django.contrib.admin.views.main import *
                  
                  class MyUserCreationForm(UserCreationForm):
                      class Meta:
                          model = User
                          fields = ('username', 'email',)
                  class UserAdmin(admin.ModelAdmin):
                      add_form = MyUserCreationForm
                  
                  admin.site.unregister(User)
                  
                  class MyUserAdmin(UserAdmin):
                      add_form = MyUserCreationForm
                      add_fieldsets = (
                          (None, {
                              'classes': ('wide',),
                              'fields': ('username', 'email', 'password1', 'password2')}
                          ),
                      )
                  
                  
                  
                  
                  admin.site.register(User, MyUserAdmin)
                  

                  推荐答案

                  如果你查看 django.contrib.auth admin.py,你会看到 UserAdmin 类将 add_form 指定为 UserCreationForm.

                  If you look in django.contrib.auth admin.py, you'll see that the UserAdmin class specifies the add_form as UserCreationForm.

                  UserCreationForm 仅包含来自 User 模型的用户名"字段.

                  UserCreationForm only includes the 'username' field from the User model.

                  由于您提供自己的 UserAdmin,因此您只需将 add_form 覆盖为自定义 UserCreationForm,其中包含使信号正常工作所需的字段.

                  Since you're providing your own UserAdmin, you can just override the add_form to a custom UserCreationForm that includes the fields you need to make your signal work properly.

                  希望对你有所帮助.

                  这是来自 contrib.auth forms.py 的 UserCreationForm:

                  Here's the UserCreationForm from contrib.auth forms.py:

                  class UserCreationForm(forms.ModelForm):
                      """
                      A form that creates a user, with no privileges, from the given username and password.
                      """
                      username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[w.@+-]+$',
                          help_text = _("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."),
                          error_messages = {'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
                      password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
                      password2 = forms.CharField(label=_("Password confirmation"), widget=forms.PasswordInput,
                          help_text = _("Enter the same password as above, for verification."))
                  
                      class Meta:
                          model = User
                          fields = ("username",)
                  
                      def clean_username(self):
                          username = self.cleaned_data["username"]
                          try:
                              User.objects.get(username=username)
                          except User.DoesNotExist:
                              return username
                          raise forms.ValidationError(_("A user with that username already exists."))
                  
                      def clean_password2(self):
                          password1 = self.cleaned_data.get("password1", "")
                          password2 = self.cleaned_data["password2"]
                          if password1 != password2:
                              raise forms.ValidationError(_("The two password fields didn't match."))
                          return password2
                  
                      def save(self, commit=True):
                          user = super(UserCreationForm, self).save(commit=False)
                          user.set_password(self.cleaned_data["password1"])
                          if commit:
                              user.save()
                          return user
                  

                  注意 fields = ("username",) 元组,它排除了 User 模型上的所有其他字段.你需要这样的东西:

                  Notice the fields = ("username",) tuple which excludes all other fields on the User model. You need something like:

                  class MyUserCreationForm(UserCreationForm):
                      class Meta:
                          model = User
                          fields = ('username', 'email',)
                  

                  然后您可以将其用作自定义 UserAdmin 中的 add_form:

                  then you can use that as the add_form in your custom UserAdmin:

                  class UserAdmin(admin.ModelAdmin):
                      add_form = MyUserCreationForm
                  

                  在我所在的地区已经很晚了,但我会看看我明天能否为你弄到一个工作样本.

                  It's pretty late in my part of the world, but I'll see if I can get a working sample for you tomorrow.

                  好的,这是您需要进行的必要更改才能完成这项工作.我已经使用 Django 1.3 对其进行了测试:

                  Ok, here's the necessary changes you'll need to make to make this work. I've tested it using Django 1.3:

                  from django.contrib.auth.forms import UserCreationForm
                  from django.contrib.auth.admin import UserAdmin
                  from django.contrib.auth.models import User
                  from django import forms
                  
                  admin.site.unregister(User)
                  
                  class MyUserAdmin(UserAdmin):
                      add_form = MyUserCreationForm
                      add_fieldsets = (
                          (None, {
                              'classes': ('wide',),
                              'fields': ('username', 'email', 'password1', 'password2')}
                          ),
                      )
                  
                  admin.site.register(User, MyUserAdmin)
                  

                  我没有看到 UserAdmin 最初有 add_fieldset 属性.这就是添加表单中没有显示电子邮件字段的原因.

                  I didn't see that the UserAdmin had an add_fieldset property initially. That's why the email field wasn't displaying in the add form.

                  这篇关于如何通过电子邮件强制执行 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中的另一个文件中查找一个文本文件的条目)
                • <i id='iBemw'><tr id='iBemw'><dt id='iBemw'><q id='iBemw'><span id='iBemw'><b id='iBemw'><form id='iBemw'><ins id='iBemw'></ins><ul id='iBemw'></ul><sub id='iBemw'></sub></form><legend id='iBemw'></legend><bdo id='iBemw'><pre id='iBemw'><center id='iBemw'></center></pre></bdo></b><th id='iBemw'></th></span></q></dt></tr></i><div id='iBemw'><tfoot id='iBemw'></tfoot><dl id='iBemw'><fieldset id='iBemw'></fieldset></dl></div>
                    <tbody id='iBemw'></tbody>

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

                    <tfoot id='iBemw'></tfoot>

                    1. <legend id='iBemw'><style id='iBemw'><dir id='iBemw'><q id='iBemw'></q></dir></style></legend>

                        • <bdo id='iBemw'></bdo><ul id='iBemw'></ul>