视图.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))
模型.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 行。知道会出现什么问题。