2017-12-14 4 views
1

私は2つのことが必要なDjangoアプリケーションを構築しています。まず、ユーザーがアクセスできるグループのセットが必要です。このグループのセットでは、テンプレートは各グループのボタンを作成することになっています。次に、以前に見つかったグループのグループに関連するManyToManyである "Property"オブジェクトのセットが必要です(これは、ユーザーがアクセスできるグループに割り当てられているすべてのプロパティが必要です)。これらのプロパティのそれぞれについて、ボタンが作成されます。私はこれを構築している間にlocalhostの管理者ユーザと作業しています。テストプロパティを持つテストグループを作成し、そのグループに管理者ユーザを割り当てました。しかし、Djangoはこれを検出していないようです。関連するコードは、以下である:現在のユーザーに属するグループを取得できません

views.py

from __future__ import unicode_literals 
from django.shortcuts import render 
from UsageData.models import Property, User, Group, MeterData 
from django.contrib import auth  

def reports_proerty_list(request): 

    if request.user.is_authenticated(): 

     current_user_groups = Group.objects.filter(id__in=request.user.groups.all()) 
     current_user_properties = Property.objects.filter(groups__in=current_user_groups) 

     return render(request, 'App/App-Template-Page.html') 

    else: 

     # The user is redirected to the sign-in page 

のApp-テンプレートPage.html

 <div class="row"> 

     {% if current_user_groups %} 
      {% for group in current_user_groups %} 
      <div class="col-sm-2 col-md-2 col-lg-2 text-center"> 
       <button class="tab-selector inactive" id="{{ group.id }}">{{ group.name }}</button> 
      </div> 
      {% endfor %} 
     {% else %} 
      <div class="col-sm-2 col-md-2 col-lg-2 text-center"> 
       <button class="tab-selector inactive" id="{{ group.id }}">Group 1</button> 
      </div> 
      <div class="col-sm-2 col-md-2 col-lg-2 text-center"> 
       <button class="tab-selector inactive" id="{{ group.id }}">Group 2</button> 
      </div> 
      <div class="col-sm-2 col-md-2 col-lg-2 text-center"> 
       <button class="tab-selector inactive" id="{{ group.id }}">Group 3</button> 
      </div> 
      <div class="col-sm-2 col-md-2 col-lg-2 text-center"> 
       <button class="tab-selector inactive" id="{{ group.id }}">Group 4</button> 
      </div> 
      <div class="col-sm-2 col-md-2 col-lg-2 text-center"> 
       <button class="tab-selector inactive" id="{{ group.id }}">Group 5</button> 
      </div> 
      <div class="col-sm-2 col-md-2 col-lg-2 text-center"> 
       <button class="tab-selector inactive" id="{{ group.id }}">Group 6</button> 
      </div> 
     {% endif %} 
     </div> 

私が抱えている問題であることの{%他%}部ページが読み込まれるとテンプレートが表示されます。これは、current_user_groupsが存在しないことを意味します。なぜこれが起こっているのかも知れませんか?私は自然に、これが最初に機能しなくてもPropertyオブジェクトを使ってやりたいことを進めることはできません。ありがとう!

答えて

2

あなたはまた、このラインは、うわー私は

current_user_groups = request.user.groups.all() 
+0

current_user_groups = Group.objects.filter(id__in=request.user.groups.all()) 

を簡素化することができrender機能

return render(request, 'App/App-Template-Page.html',{ 'current_user_groups': current_user_groups, 'current_user_properties': current_user_properties }) 

の文脈の中にあなたの変数のいずれかに合格していませんこれを覚えていないためにばかげている。どうもありがとうございます!私はできるだけ早くあなたの答えを受け入れるでしょう:) – paolompmojica

+0

このような問題はありません:) – iklinac

関連する問題