2014-01-12 184 views
1

我想使用自定義模型來擴展我的用戶配置文件。不幸的是,無論何時訪問配置文件所在的頁面,我都會收到'元組'對象沒有任何屬性'更新'「元組」對象沒有屬性「更新」

我試圖自己解決這個問題,但經過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' 

另外,我運行在一個虛擬的ENV這個項目。

任何幫助,將不勝感激。

回答

3

在您看來args應字典不會元組:

# args =() 
args = {} 
+0

經典Python的問題,呵呵 – danuker

2

在從一個元組的字典視圖代碼更改:

args =() 

到:

args = {} 
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 
相關問題