2017-03-22 7 views
0

問題は、私は条件が動作していない場合があります。私がprint(p.user) & print(username)を書くとき、p.userとusernameの両方が同じ値を返しますが、まだそれは一致しません。また、私が文脈を印刷するとき、それは常に著者にとって偽の値です。django/pythonで文字列が一致しません

def post_full(request, username, slug): 

    post = Post.objects.filter(slug = slug) 
    instance = get_object_or_404(Post, slug = slug) 
    files = file.objects.filter(Post = instance) 
    context = {'post': instance,'file':files} 
    for p in post: 
     if p.user==username: 
      context.update({'author': True}) 
     else: 
      context.update({'author': False}) 
    try: 
     slug = request.POST["query"] 
     if slug: 
      print(slug) 
      query = slugify(slug) 
      return HttpResponseRedirect('/search/%s/'%query) 
    except Exception as e: 
     pass 
    return render(request, "post_detail.html", context) 
+1

try 'username.strip()' –

+0

これは動作しません。 –

+0

も ​​'p.user'で削除してください。また、大文字小文字の –

答えて

1

平等チェック中にスペースを取り除いてみてください。

if p.user.strip()==username.strip(): 

そしてまた、それは、DBにデータを保存する前にstripを適用しない方が良いでしょう。このようにして、等価チェック中にdbオブジェクトにstripを適用する必要はありません。

string.strip()は、不要な先頭と末尾のスペースをすべて削除します。

関連する問題