[英]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.