【问题标题】:Making a Django Profile-change Form Autofill制作 Django 配置文件更改表单自动填充
【发布时间】:2015-01-14 17:36:58
【问题描述】:

我为自定义用户和视图编写了一个模型,允许我添加新用户、查看他们的个人资料信息以及登录/退出。 但是,在为我的用户编写视图以更新他们的个人资料信息时,我发现正在显示的表单要求用户再次填写他们的所有详细信息,即使他们不想更改该特定详细信息。例如,如果用户只想更改姓氏,则必须重新输入电子邮件、密码、名字等。 有没有办法让我自动填写此表单,并且用户可以更改他们需要的内容? 我在下面附上了我的表格和视图代码。非常感谢任何帮助!

views.py

@login_required(login_url='/login/')
def personal(request):
    """
    Personal data of the user profile
    """
    profile = MyUser.get_full_name #This returns the email address

    if request.method == "POST":
        form = ProfileForm(request.POST, instance=profile)
        if form.is_valid():
            form.save()
            messages.add_message(request, messages.INFO, _("Your profile information has been updated successfully."))

    else:
        form = ProfileForm

    template = "update_profile.html"
    data = { 'section': 'personal', 'form': form, }
    return render_to_response(template, data, context_instance=RequestContext(request))

forms.py

class ProfileForm(forms.ModelForm):
    """
    Profile Form. Composed by all the Profile model fields.
    """
    class Meta:
        model = MyUser
        exclude = ('is_active', 'is_admin', 'last_login')

实施解决方案后出错

Environment:


Request Method: GET
Request URL: http://localhost:8000/friends/update/

Django Version: 1.6.5
Python Version: 2.7.7
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'friends')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "D:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Python27\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "D:\Uni\_Project\friend_finder_V2\mysite\friends\views.py" in personal
  85.         form = ProfileForm(instance=profile)
File "D:\Python27\lib\site-packages\django\forms\models.py" in __init__
  315.             object_data = model_to_dict(instance, opts.fields, opts.exclude)
File "D:\Python27\lib\site-packages\django\forms\models.py" in model_to_dict
  124.     opts = instance._meta

Exception Type: AttributeError at /friends/update/
Exception Value: 'function' object has no attribute '_meta'

【问题讨论】:

    标签: python django django-forms django-admin django-views


    【解决方案1】:

    您也可以使用instance kwarg 在 GET 上将当前配置文件传递给您的表单(我看到您已经在 POST 上传递了它)。此外,不要忘记在使用 POST 更改数据库后执行 HTTP 重定向。像这样的:

    form = ProfileForm(request.POST, instance=profile) 如果 request.method == 'POST: 如果 form.is_valid(): form.save() messages.add_message(request, messages.INFO, _("您的个人资料已成功更新。")) # TODO 你需要在这里做一个重定向 别的: 形式= ProfileForm(实例=配置文件)

    【讨论】:

    • 试一试后,我收到一条错误消息:“'function' object has no attribute '_meta'”。关于解决这个问题的任何想法?
    • 请使用完整的堆栈跟踪更新您的原始消息。似乎 django 需要一个对象,而您正在传递一个函数...
    • 已经更新了,问题好像出在:profile = MyUser.get_full_name 这一行我想。
    猜你喜欢
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 2011-09-12
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    相关资源
    最近更新 更多