django问题将数据库条目与if语句进行比较
我的 emp/views.py 中有以下代码:
@ login_required
def peoples(request):
list_a = []
if request.user.username == People.objects.get(account=request.user.username):
angestellte_list = People.objects.all()
return render_to_response(("emp/people.html"), {'list_a': list_a})
else:
return HttpResponse("Nope, it doesn't works")
我总是得到 else 语句的打印输出。但为什么?
如果我在 if 语句中使用明确的示例,它就会起作用,例如
if request.user.username == 'bob':
....
....
我不明白它。
编辑:
class People(models.Model):
empid = models.DecimalField(max_digits=19, decimal_places=0, primary_key=True)
firstname = models.CharField(max_length=20, blank=True)
SHIFT_CHOICES=( ('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D'), ('N', 'N'),)
shift = models.CharField(max_length=1, choices=SHIFT_CHOICES, blank=True)
account = models.CharField(max_length=20, blank=True)
def __unicode__(self):
return self.lastname
class Meta:
db_table = u'people'
第二次编辑:
很抱歉回答晚了,但我最近几天没有太多时间编码。我发现了问题。 People.objects.get(account=request.user.username) 获取名称,但删除第一个字符并将第二个字符大写,只有 request.user.username 获取确切的名称。 一个例子:
request.user.username = pstefan
Employee.objects.get(account=request.user.username) = Stefan
我这样解决了它:
username = str(request.user.username)[1:]
compare = str(People.objects.get(account=request.user.username))
user_end = username.replace(username[0], compare[0], 1)
if user_end == compare:
....
它有效,但我知道这不是最好的方法。有什么建议吗?
i have the following code in my emp/views.py:
@ login_required
def peoples(request):
list_a = []
if request.user.username == People.objects.get(account=request.user.username):
angestellte_list = People.objects.all()
return render_to_response(("emp/people.html"), {'list_a': list_a})
else:
return HttpResponse("Nope, it doesn't works")
I always get the printout of the else statement. But why?
If I use a explicit example in the if statement it works, e.g.
if request.user.username == 'bob':
....
....
I don't understand it.
edit:
class People(models.Model):
empid = models.DecimalField(max_digits=19, decimal_places=0, primary_key=True)
firstname = models.CharField(max_length=20, blank=True)
SHIFT_CHOICES=( ('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D'), ('N', 'N'),)
shift = models.CharField(max_length=1, choices=SHIFT_CHOICES, blank=True)
account = models.CharField(max_length=20, blank=True)
def __unicode__(self):
return self.lastname
class Meta:
db_table = u'people'
2nd edit:
Sorry for the late answering, but i hadn't much time for coding the last days. I found the problem. People.objects.get(account=request.user.username) takes the name but it deletes the first character and capitalizes the second one and only request.user.username takes the exact name.
An example:
request.user.username = pstefan
Employee.objects.get(account=request.user.username) = Stefan
I solved it this way:
username = str(request.user.username)[1:]
compare = str(People.objects.get(account=request.user.username))
user_end = username.replace(username[0], compare[0], 1)
if user_end == compare:
....
It works, but i know it isn't the best way. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在将用户名 (CharField) 与 Person 对象进行比较。尝试:
或者更好的是你可以使用:
you are comparing a username (CharField) to an Person object. Try :
or better still you could use:
您需要确定 Peoples 对象上的“account”属性是什么以及它与用户的关系。如果您可以发布 Peoples 模型,可能会更容易确定解决方案。
You need to determine what the "account" attribute is on the Peoples object and how it relates to user. If you can post the Peoples model, it may be easier to determine the solution.