简体   繁体   English

如何检查存储为 ForeignKey 字段的用户名和 request.user.username 在 Django 中是否相等?

[英]How do I check whether username stored as a ForeignKey field and request.user.username are equal in Django?

I am making a personal diary app in Django.我正在 Django 中制作一个个人日记应用程序。 Over there I have made an app 'note'.在那里,我做了一个应用程序“注释”。 The models.py of note app looks like this: note 应用的 models.py 如下所示:

from django.db import models
from datetime import datetime
from django.contrib.auth.models import User

class Note(models.Model):
    image=models.ImageField(upload_to='images/')
    title=models.CharField(default="",max_length=20)
    body=models.TextField(default="",max_length=800)
    date=models.DateField(default=datetime.now())
    publisher=models.ForeignKey(User,on_delete=models.CASCADE,default="")

    def __str__(self):
        return self.title

Now when a user logs in, in his home page I want to show the note entries made by him only.现在,当用户登录时,在他的主页中我只想显示他所做的笔记条目。 The views.py and home.html are given below: views.py 和 home.html 如下:

views.py:视图.py:

@login_required
def home(request):
    notes=Note.objects    
    return render(request,'home.html',{'notes':notes})

home.html:主页.html:

{% extends 'basic.html' %}
{% load static %}
{% block content %}
{% if error %}
{{error}}
{% endif %}
   {% for n in notes.all  %}
   {% ifequal request.user.username n.publisher  %}
   <div class="container">
   <a href="{% url 'note_app:detail' n.id %}"><h3>{{n.title}}</h3></a>
   <p>{{n.body}}</p>
   <footer>{{n.date}}</footer>
   </div>
   {% endifequal %}
   {% endfor %}
{% endblock %}

As you can see in my home.html I am iterating through all the objects of class Note.正如您在 home.html 中看到的,我正在遍历 Note 类的所有对象。 When the ifequal statement is not there it is showing all the entries of all users but when I am putting the ifequal statement to make sure that entries made by the logged in user is only shown it is not showing anything.ifequal语句不存在时,它会显示所有用户的所有条目,但是当我放置ifequal语句以确保仅显示登录用户所做的条目时,它不显示任何内容。 Can somebody tell me how to fix this?有人可以告诉我如何解决这个问题吗? I'm an absolute beginner here in web development and I appologise in advance for a stupid question but I'm stuck.我是网络开发领域的绝对初学者,我提前为一个愚蠢的问题道歉,但我被卡住了。

I believe your mistake it's in views.py try to use:我相信你的错误是在 views.py 尝试使用:

notes=Note.objects.all()    

instead of:代替:

notes=Note.objects    

You're just missing the second parameter to {% ifequal %} .您只是缺少{% ifequal %}的第二个参数。 Right now, you're not comparing anything to anything.现在,您没有将任何东西与任何东西进行比较。 Try this:尝试这个:

home.html:主页.html:

{% extends 'basic.html' %}
{% load static %}
{% block content %}
{% if error %}
{{error}}
{% endif %}
   {% for n in notes.all  %}
   {% ifequal request.user n.publisher %}
   <div class="container">
   <a href="{% url 'note_app:detail' n.id %}"><h3>{{n.title}}</h3></a>
   <p>{{n.body}}</p>
   <footer>{{n.date}}</footer>
   </div>
   {% endifequal %}
   {% endfor %}
{% endblock %}

That said, I should note that you don't have to do this in your template.也就是说,我应该注意到您不必在模板中执行此操作。 It'll be faster if you do it in the database.如果您在数据库中执行它会更快。 You can change your views.py to this:您可以将您的views.py更改为:

@login_required
def home(request):
    notes = Note.objects.filter(publisher=request.user)
    return render(request,'home.html',{'notes':notes})

at which point you won't even need the {% ifequal %} tag in the template.此时您甚至不需要模板中的{% ifequal %}标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django单元测试,自定义权限和request.user.username - Django unit tests, custom permission and request.user.username 如何将登录用户的用户名添加到 django 中的模型字段 - How do I add username of logged in user to model field in django Django:如何在SessionWizardView中请求用户名? - Django: how do i request username in SessionWizardView? 如何在不扩展用户 Model 的 Django ModelForm 中自定义默认用户 Model 的用户名字段的显示? - How do I customize the display of username field of default User Model in Django ModelForm without extending User Model? Django:获取身份验证用户外键的用户名 - Django: grab username of auth user foreignkey 如何在Django中通过user_id获取用户名? - How do I get username by user_id in Django? 通过用户名检查用户是否登录 django - Check if user is logged in or not django by username 如何在Django中使用用户名填充字段? - How can I fill a field in Django with the username? 如何获取当前用户的用户名并将其分配给django表单中的某个字段? - How to get the username of the current user and assign it to a certain field in a form in django? 我如何使用 django 中的额外字段(除用户名/ email 和密码之外)验证任何用户登录 - How can i authenticate any user for login with an extra field (other then username/ email and password) in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM