我想使用自定義模型來擴展我的用戶配置文件。不幸的是,無論何時訪問配置文件所在的頁面,我都會收到'元組'對象沒有任何屬性'更新'「元組」對象沒有屬性「更新」
我試圖自己解決這個問題,但經過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這個項目。
任何幫助,將不勝感激。
經典Python的問題,呵呵 – danuker