ビュー.py
def add_phone(request):
user=request.user
try:
phone = Phone_info.objects.get(user=user.id)
except Phone_info.DoesNotExist:
phone = None
phoneForm = PhoneForm({'user':request.user})
phone = Phone_info.objects.get(user=request.user)
phoneForm = PhoneForm(instance=phone)
if request.method=='POST':
phoneForm = PhoneForm(request.POST,instance=phone)
if phoneForm.is_valid():
phone=phoneForm.save(commit=False)
phone.save()
return redirect('/member/contact-list/')
return render_to_response('incident/add_phone.html',
{
'about_menu': True,
'PhoneForm' :phoneForm
},
context_instance=RequestContext(request))
models.py は
class Phone_info(models.Model):
user = models.ForeignKey(User, null=True)
name1 = models.CharField('Name', max_length=100, null=True, blank=True)
number1 = models.CharField('Number',max_length=20, null=True, blank=True)
name2 = models.CharField('Name', max_length=100, null=True, blank=True)
number2 = models.CharField('Number', max_length=20, null=True, blank=True)
トレースバック
Environment:
Request Method: GET
Request URL: http://192.168.100.10/member/add-phone/
Django Version: 1.3.7
Python Version: 2.7.0
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.humanize',
'django.contrib.staticfiles',
'south',
'collect',
'incident']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware')
編集:
Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/root/Projects/ir/incident/views.py" in add_phone
648. phone = Phone_info.objects.get(user=request.user)
File "/usr/lib/python2.7/site-packages/django/db/models/manager.py" in get
132. return self.get_query_set().get(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py" in get
349. % self.model._meta.object_name)
Exception Type: DoesNotExist at /member/add-phone/
Exception Value: Phone_info matching query does not exist.
次のエラーが発生して、データベースにデータを挿入または更新できません」Phone_info matching query does not exist"
ここで何がうまくいかなかったのか、誰かが私に提案できますか。編集: トレースバックを更新しました。エラーは 6 行目にあります。何が問題になるか考えてください。