我想拥有的内容:迭代发布数据的每个用户,如果帖子的用户名等于连接的用户名,则写入“foo”。
问题:我认为o.user不会在{%%}或if语句之间提供信息。 有趣的是o.user确实给了我ifequal之外的信息(在p标签中)
我没有发现我做的错误......
<div class="secondblock" >
{% for o in p %}
{% ifequal user.username o.user %} Foo
{% endifequal%}
<p style="box-shadow: 1px 1px 3px rgb(216, 216, 216); background-color:white; padding : 10px;margin-top:5px;text-align:left; max-height:75px; overflow:auto; border: 1px solid rgb(229, 229, 229); "><span style="color: red;font-weight: 400; cursor:pointer;">{{o.user|upper }}</span> <span style="font-size:0.9em; float:right;">{{o.createdat}}</span>
</br>
</br> {{o.postit}} </p>
{% endfor %}
</div>
formmodel:
class StoryForm(ModelForm):
class Meta:
model = Topic
exclude =('user','createdat','comment')
模特:
class Topic(models.Model):
user = models.ForeignKey(User)
postit = models.TextField(max_length=300)
comment = models.TextField(max_length=300)
createdat = models.DateTimeField(default=timezone.now)
'''createdat = models.DateTimeField(auto_now_add=True)
updatedat = models.DateTimeField(auto_now=True)
def created(self):
return self.createdat
'''
def updated(self):
return self.updatedat
def timer(self):
return self.createdat
def username(self):
return self.user
def __str__(self): # __unicode__ on Python 2
return self.postit
我的观点:
def chat(request):
'''
if not request.user.is_authenticated():
return redirect('/login/?next=%s' % request.path)
return render(request,'chat/chat.html')
'''
if request.method == 'POST':
form = StoryForm(request.POST)
if form.is_valid():
storyi = form.save(commit=False)
storyi.user = request.user
storyi.save()
return HttpResponseRedirect('/chat/')
else:
form=StoryForm()
try:
p = Topic.objects.all().order_by('-createdat')[:5]
#p.encode('ascii', 'ignore')
except ObjectDoesNotExist:
p= "don't try it again"
return render(request,'chat/chat.html',{'form': form, 'p':p})
答案 0 :(得分:2)
现在您要将对象用户与用户名进行比较,请尝试这样:
{% ifequal user.username o.user.username %}
...