'tuple'对象没有属性'update'

时间:2014-01-12 20:03:07

标签: python django

我想使用自定义模型扩展我的用户配置文件。不幸的是,每当我访问配置文件所在的页面时,我收到'tuple'对象没有属性'update'

我自己试图解决这个问题,但是经过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)

app 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'

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

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:3)

在您看来args应该是dict not tuple:

# args = ()
args = {}

答案 1 :(得分:2)

在您的视图中,代码从元组更改为dict:

args = ()

为:

args = {}

答案 2 :(得分:1)

我不确定你要做什么,但问题是:

args = ()   # This makes args into an empty tupple
args.update(csrf(request)) # This tries to invoke a missing update method

args['form'] = form # This looks like args should be a dictionary

尝试:

args = {}   # This makes args into an empty dictionary
args.update(csrf(request)) # This tries to invoke a update which a dict has

args['form'] = form