Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django中的allauth帐户编辑_Python_Django_Django Allauth - Fatal编程技术网

Python django中的allauth帐户编辑

Python django中的allauth帐户编辑,python,django,django-allauth,Python,Django,Django Allauth,我正在用django构建一个应用程序,我决定选择allauth身份验证。现在我有一个个人资料页面,我希望用户能够编辑他们的帐户,包括Bios,姓氏,名字和类似的东西,并可能包括一个显示图片,但我输入了一些代码,我得到了一个错误,我已经在它的库存很长时间。错误是 错误 Traceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.

我正在用django构建一个应用程序,我决定选择allauth身份验证。现在我有一个个人资料页面,我希望用户能够编辑他们的帐户,包括Bios,姓氏,名字和类似的东西,并可能包括一个显示图片,但我输入了一些代码,我得到了一个错误,我已经在它的库存很长时间。错误是

错误

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/core/management/__init__.py", line 337, in execute
    django.setup()
  File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/Olar/Desktop/arbithub/src/profiles/models.py", line 27, in <module>
    class Profile(models.Model):
  File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/db/models/base.py", line 124, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/db/models/base.py", line 331, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/db/models/options.py", line 206, in contribute_to_class
    raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()))
TypeError: 'class Meta' got invalid attribute(s): fields,model
Forms.py

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

# Create your models here.
class Profile(models.Model):
    username = models.CharField(max_length=125)
    email = models.EmailField(max_length=125)
    first_name = models.CharField(max_length=125)
    last_name = models.CharField(max_length=125)

    class Meta:
        model = User
        fields = ('username', 'email', 'first_name', 'last_name')

    def __str__(self):
        return self.username
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from profiles.models import Profile

class UpdateProfile(forms.ModelForm):
    username = forms.CharField(required=True)
    email = forms.EmailField(required=True)
    first_name = forms.CharField(required=False)
    last_name = forms.CharField(required=False)

    class Meta:
        model = User
        fields = ('username', 'email', 'first_name', 'last_name')

    def clean_email(self):
        username = self.cleaned_data.get('username')
        email = self.cleaned_data.get('email')

        if email and User.objects.filter(email=email).exclude(username=username).count():
            raise forms.ValidationError('This email address is already in use. Please supply a different email address.')
        return email

    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']

        if commit:
            user.save()

        return user
这是我的代码上面的错误,我只是不能让我的头周围的问题是什么。任何帮助将不胜感激

删除属性

class Profile(models.Model):
    # ... your code here
    class Meta: # And this line need to remove
        model = User # And this line need to remove
        fields = ('username', 'email', 'first_name', 'last_name') # this line need to remove
在模型元中,您只能使用

如果要扩展默认的django用户模型,请阅读删除属性

class Profile(models.Model):
    # ... your code here
    class Meta: # And this line need to remove
        model = User # And this line need to remove
        fields = ('username', 'email', 'first_name', 'last_name') # this line need to remove
在模型元中,您只能使用

如果要扩展默认的django用户模型,请阅读此代码

class Meta:
    model = User
    fields = ('username', 'email', 'first_name', 'last_name')
不属于您的模型。因此,
model
字段都是模型的
Meta
类的无效关键字参数

编辑以回应评论:

如果要添加更多字段,请将其添加到模型中:

class MyModel(models.Model):
    my_new_field = models.IntegerField()
然后,在模型表单中,将
my\u new\u字段
添加到
字段
参数中。请注意,除非您想做超出模型所提供范围的事情,否则只需将其添加到字段的
元组中即可。换句话说,您不需要添加:

my_new_field = forms.IntegerField()
ModelForm
为您神奇地处理此问题,如文档所示:

这是使用模型窗体和模型视图集等功能的优点。它通过重复使用模型的模式、推断字段和字段类型来保持代码的干燥

我希望这能把事情弄清楚。

这段代码

class Meta:
    model = User
    fields = ('username', 'email', 'first_name', 'last_name')
不属于您的模型。因此,
model
字段都是模型的
Meta
类的无效关键字参数

编辑以回应评论:

如果要添加更多字段,请将其添加到模型中:

class MyModel(models.Model):
    my_new_field = models.IntegerField()
然后,在模型表单中,将
my\u new\u字段
添加到
字段
参数中。请注意,除非您想做超出模型所提供范围的事情,否则只需将其添加到字段的
元组中即可。换句话说,您不需要添加:

my_new_field = forms.IntegerField()
ModelForm
为您神奇地处理此问题,如文档所示:

这是使用模型窗体和模型视图集等功能的优点。它通过重复使用模型的模式、推断字段和字段类型来保持代码的干燥


我希望这会把事情弄清楚。

对于您的最后一个错误,需要替换

class UpdateProfile(forms.ModelForm):
    # your code ...
    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        #           ^^^^^^^^^^^^^^^^^


有关上一个错误的详细信息,请更换

class UpdateProfile(forms.ModelForm):
    # your code ...
    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        #           ^^^^^^^^^^^^^^^^^



更多详细信息

因此模型中应该没有元我得到了以下错误'TypeError:'class Meta'得到了无效属性:模型'I只是删除了字段。并且
TypeError:“class Meta”得到了无效的属性:model
出现了我编辑答案,在您的情况下需要删除class METAOk它现在可以工作了,但是如果我想添加更多的字段,比如Bios和图片上传选项。我是否只是将该字段添加到概要文件模型中?因此模型中不应该有Meta我得到了以下错误'TypeError:'class Meta'得到了无效属性:model'我只是删除了字段。并且
TypeError:“class Meta”得到了无效的属性:model
出现了我编辑答案,在您的情况下需要删除class METAOk它现在可以工作了,但是如果我想添加更多的字段,比如Bios和图片上传选项。我是否只是将该字段添加到配置文件模型中?好的,它现在可以工作,但如果我想添加更多字段,如Bios和图片上载选项。我是否只是将字段添加到配置文件模型中?谢谢。请注意,我在浏览器中遇到以下错误`异常值:未定义全局名称'UpdateProfile',`我们需要更多信息来解决此问题,但我猜您可能忘记导入该类了?是的,我这样做了,效果很好。然后,另一个错误说,
global name'RegistrationForm'
未定义,然后我将其导入到我的视图中,我从中得到了此错误
。forms import RegistrationForm ImportError:cannot import name RegistrationForm
您的
forms.py
中是否存在该类?现在可以工作了,但如果我想添加更多的字段,如Bios和图片上传选项。我是否只是将字段添加到配置文件模型中?谢谢。请注意,我在浏览器中遇到以下错误`异常值:未定义全局名称'UpdateProfile',`我们需要更多信息来解决此问题,但我猜您可能忘记导入该类了?是的,我这样做了,效果很好。然后另一个错误说,
global name'RegistrationForm'
未定义,然后我将其导入到我的视图中,我从中得到了此错误
。表单导入注册表单导入错误:无法导入名称注册表单
您的
表单.py
中是否存在该类?谢谢,我不敢相信我在我试图修复以前的错误谢谢我不敢相信我在修复以前的错误时犯了这样的错误