django问题将数据库条目与if语句进行比较

发布于 2024-11-26 14:50:38 字数 1637 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风渺 2024-12-03 14:50:38

您正在将用户名 (CharField) 与 Person 对象进行比较。尝试:

if request.user == People.objects.get(...).account:

或者更好的是你可以使用:

import django.shortcuts as shortcuts
shortcuts.get_object_or_404(People, account=request.username)

you are comparing a username (CharField) to an Person object. Try :

if request.user == People.objects.get(...).account:

or better still you could use:

import django.shortcuts as shortcuts
shortcuts.get_object_or_404(People, account=request.username)
鲜血染红嫁衣 2024-12-03 14:50:38

您需要确定 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文