我知道问了类似的问题,但在我的情况下,我在部署到pythonanywhere.com后得到了RelatedObjectDoesNotExist。
" RelatedObjectDoesNotExist:用户没有满足感。"
但是同样的代码在本地机器上工作正常。
Model.py
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
class gwDashboard(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
htmlname = models.CharField(max_length=100)
def __str__(self): # __unicode__ on Python 2
return self.htmlname
class userInformation(models.Model):
user = models.ForeignKey(User)
DbName = models.CharField(max_length=200)
DbTable = models.CharField(max_length=200)
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.CharField(max_length=100)
class eisfiles(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
htmlname = models.CharField(max_length=100)
meterId = models.CharField(max_length=100)
esiid = models.CharField(max_length=100)
metermultiplier = models.CharField(max_length=100)
DbName = models.CharField(max_length=100,default='800WILCREST')
DbTable = models.CharField(max_length=100)
def __str__(self):
return self.htmlname
def __str__(self):
return self.DbName
class Member(models.Model):
ID = models.AutoField(primary_key=True)
FIRST_NAME = models.CharField('First name', max_length=50)
LAST_NAME = models.CharField('Last name', max_length=50)
# Using multi table inheritance - automaticly creates one to one field
class MemberDetail(Member):
DATE_OF_BIRTH = models.DateField('Date of birth')
EMAIL = models.EmailField('E-mail')
PHONE = models.CharField('Phone', max_length=15)
Views.py
def home(request):
userName = request.user
u = User.objects.get(username=userName);
DbName=u.eisfiles //THIS LINE OF CODE THROWS ERROR
try:
DbName=u.eisfiles //THIS LINE OF CODE THROWS ERROR
DbName=u.eisfiles.DbName
direct=''
return render_to_response(
'registration/result.html',
{ 'kWhHeatMap': kWhHeatMap, 'temHeatMap':temHeatMap,"histKwhChart":histKwhChart,'officeBuilding':officeBuilding},
)
except User.eisfiles.RelatedObjectDoesNotExist:
pass
return render_to_response(
'registration/result.html',
{ 'kWhHeatMap': "", 'temHeatMap':"","histKwhChart":"",'officeBuilding':""},
)
# except User.AttributeError:
# return HttpResponse('something went wrong!! try again')
finally:
pass
答案 0 :(得分:4)
您可能想要检查您尝试检索的用户是否确实存在。
userName = request.user # ??? This is the user object not the username
u = User.objects.get(username=userName)
您的查询似乎存在问题。请改为:
user = request.user
u = User.objects.get(id=user.id)
DbName = u.eisfiles.DbName