Django signup: set username to provided email address and save?

  • 1,000
  • Tác giả: admin
  • Ngày đăng:
  • Lượt xem: 1
  • Tình trạng: Còn hàng

Short question (no actual need đồ sộ read the rest): I have read multiple threads/tutorials (e.g. this one) on how đồ sộ let users login via tin nhắn. And I was wondering: why can't I just keep things simple and ask for an tin nhắn address upon signup and then phối the username đồ sộ that tin nhắn address and save the user?

Long part: I would create a size that is very similar đồ sộ the UserCreationForm of Django 1.8. I'm not sure why I cannot find the same code for 3.1 (which makes bầm wonder even more whether my idea might be bad practice for some reason) but basically I would tự this: create a ModelForm for the mặc định user model, phối fields = ["email"], render it manually as {{form.email}} in the template and then, in the view, I'll gọi form.save(commit=False) đồ sộ get the object, phối the username of that object đồ sộ the given tin nhắn address and save it. Orrrr don't I? :-)

EDIT

Since more source code was asked, here's basically what I meant:

forms.py

class UserCreationForm(forms.ModelForm):

    class Meta:
        model = User
        fields = ["email"]

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.username = self.cleaned_data["email"]
        if commit:
            user.save()
        return user

views.py

...
form = UserCreationForm()
return render(request, 'myapp/createuser.html', {'form': form})
...

createuser.html

{% csrf_token %} {{ size.as_p }}