Skip to main content
 首页 » 编程设计

python之“元组”对象没有属性 'update'

2025年05月04日14me-sa

我想使用自定义模型扩展我的用户配置文件。不幸的是,每当我访问配置文件所在的页面时,我都会收到“元组”对象没有属性“更新”

我曾尝试自己解决这个问题,但 2.5 小时后我突然意识到我被卡住了。

请问有没有大佬解惑一下?

型号:

from django.db import models 
from django.contrib.auth.models import User 
 
# Create your models here. 
 
class Skills(models.Model): 
description = models.CharField(max_length=3000) 
 
class UserProfile(models.Model): 
 
#Choices 
STAFF = ( 
    ('AD', 'Super User'), 
    ('MA', 'Manager'), 
) 
DEPT = ( 
    ('TD', 'Tech Service'), 
    ('CS', 'Customer Service'), 
) 
user = models.OneToOneField(User, blank=False, unique=True) 
staff = models.CharField(max_length=2, choices=STAFF) 
dept = models.CharField(max_length=3, choices=DEPT) 
team = models.BooleanField(default=False) 
skill = models.ManyToManyField(Skills) 
email = models.EmailField() 
 
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0]) 

表格

from django import forms 
from models import UserProfile 
 
class UserProfileForm(forms.ModelForm): 
 
class Meta: 
    model = UserProfile 
    fields = ('staff', 'dept', 'team', 'skill', 'email') 

查看

from django.shortcuts import render_to_response 
from django.http import HttpResponseRedirect 
from django.core.context_processors import csrf 
from forms import UserProfileForm 
from django.contrib.auth.decorators import login_required 
 
@login_required 
def user_profile(request): 
if request.method == 'POST': 
    form = UserProfileForm(request.POST, instance=request.user.profile) 
    if form.is_valid(): 
        form.save() 
        return HttpResponseRedirect('/accounts') 
else: 
    user = request.user 
    profile = user.profile 
    form = UserProfileForm(instance=profile) 
 
args = () 
args.update(csrf(request)) 
 
args['form'] = form 
 
return render_to_response(profile.html, args) 

应用程序 urls.py

from django.conf.urls import patterns, include, url 
 
urlpatterns = patterns('', 
    url(r'^profile/$', 'userprofile.views.user_profile'), 
    ) 

和标准 urls.py

from django.conf.urls import patterns, include, url 
 
from django.contrib import admin 
admin.autodiscover() 
 
urlpatterns = patterns('', 
# Examples: 
# url(r'^$', 'tms_core.views.home', name='home'), 
# url(r'^blog/', include('blog.urls')), 
 
url(r'^admin/', include(admin.site.urls)), 
url(r'^accounts/', include('userprofile.urls')), 
) 

最后是追溯:

Environment: 
 
 
Request Method: GET 
Request URL: http://127.0.0.1:8000/accounts/profile/ 
 
Django Version: 1.6.1 
Python Version: 2.7.5 
Installed Applications: 
('django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'userprofile') 
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 "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response 
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs) 
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view 
  22.                 return view_func(request, *args, **kwargs) 
File "/home/user/projects/tms5/tms_core/userprofile/views.py" in user_profile 
  20.     args.update(csrf(request)) 
 
Exception Type: AttributeError at /accounts/profile/ 
Exception Value: 'tuple' object has no attribute 'update' 

另外,我正在虚拟环境中运行这个项目。

如有任何帮助,我们将不胜感激。

请您参考如下方法:

在你看来 args 应该是字典而不是元组:

# args = () 
args = {}