簡體   English   中英

在 AbstractUser 和 Django-allauth 中添加頭像

[英]add avatar in AbstractUser and Django-allauth

我的代碼工作正常,我進行遷移以向我的用戶添加頭像。

但是當我注冊時,頭像字段不顯示。

這是我的代碼:models.py

from django.db import models
from django.contrib.auth.models import AbstractUser


def get_path_name(instance, filename):
    path = 'media/avatar/'
    name = instance.user.id + "-" + instance.user.email
    path = path + name
    return path

# custom User model
class CustomUser(AbstractUser):
    avatar = models.ImageField(upload_to= get_path_name, blank=True, null=True)

我的表格:

from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.core.files.images import get_image_dimensions
from django import forms

class CustomUserCreationForm(UserCreationForm):
    class Meta:
        model = get_user_model()
        fields = ('email', 'username', 'avatar')

    def clean_avatar(self):
        avatar = self.cleaned_data['avatar']

        try:
            w, h = get_image_dimensions(avatar)

            # validate dimensions
            max_width = max_height = 100
            if w > max_width or h > max_height:
                raise forms.ValidationError(
                    u'Please use an image that is '
                    '%s x %s pixels or smaller.' % (max_width, max_height))

            # validate content type
            main, sub = avatar.content_type.split('/')
            if not (main == 'image' and sub in ['jpeg', 'pjpeg', 'gif', 'png']):
                raise forms.ValidationError(u'Please use a JPEG, '
                                            'GIF or PNG image.')

            # validate file size
            if len(avatar) > (20 * 1024):
                raise forms.ValidationError(
                    u'Avatar file size may not exceed 20k.')

        except AttributeError:
            """
            Handles case when we are updating the user profile
            and do not supply a new avatar
            """
            pass

        return avatar

class CustomUserChangeForm(UserChangeForm):
    class Meta:
        model = get_user_model()
        fields = ('email', 'username', 'avatar')

    def clean_avatar(self):
        avatar = self.cleaned_data['avatar']

        try:
            w, h = get_image_dimensions(avatar)

            # validate dimensions
            max_width = max_height = 100
            if w > max_width or h > max_height:
                raise forms.ValidationError(
                    u'Please use an image that is '
                    '%s x %s pixels or smaller.' % (max_width, max_height))

            # validate content type
            main, sub = avatar.content_type.split('/')
            if not (main == 'image' and sub in ['jpeg', 'pjpeg', 'gif', 'png']):
                raise forms.ValidationError(u'Please use a JPEG, '
                                            'GIF or PNG image.')

            # validate file size
            if len(avatar) > (20 * 1024):
                raise forms.ValidationError(
                    u'Avatar file size may not exceed 20k.')

        except AttributeError:
            """
            Handles case when we are updating the user profile
            and do not supply a new avatar
            """
            pass

        return avatar

我的模板:

{% extends '_base.html' %}
{% load crispy_forms_tags %}

{% block title %}

{% endblock title %}

{% block content %}
    <h2>Sign Up</h2>
    <form method="post">
        {% csrf_token %}
        {{ form|crispy }}
        <button class="btn btn-success" type="submit">Sign Up</button>
    </form>
{% endblock content %}

我的設置:

AUTH_USER_MODEL = 'accounts.CustomUser'

當我的注冊頁面沒有頭像時,我錯過了什么? 在此處輸入圖片說明

我試圖在模板中添加字段但似乎不起作用。 當我進入管理站點時,我沒有看到頭像字段,但遷移已經完成...

謝謝你的幫助

我找到了我的答案。 在 settings.py 中添加這個

ACCOUNT_SIGNUP_FORM_CLASS = 'myapp.forms.CustomUserCreationForm'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM