2016-04-19 3 views
0

私はDjangoの初心者です。私は自分のビューをサインインして注文するようにしています。例えば、 '[email protected]'がサインインされているとします。 Tviewは最初の行に[email protected]に関連するデータを与える必要があります。現時点では特定の順序ではありません。djangoユーザがサインインしてフィールドを注文する

Current order: for a user signed as [email protected] 
    GEL_TEACT [email protected] 
    TREAT_ACT [email protected] 

    I want to order this as: 

    Product OWNER 
    TREAT_ACT [email protected] 
    GEL_TEACT [email protected] 

私のビュー:

class PListView(ListView): 
    model = Product 

    template_name = "app/product_list.html" 

project_list_view = PListView.as_view() 

私のテンプレート:お時間を

<tbody> 
       {% for product in object_list %} 
       <tr> 
        <td><a href="{% url "product-detail" pro_id=project.pk %}">{{ product.title }}</a></td> 
        <td> 
         <a href="{% url "product-update" project_id=project.pk %}"> 
          <i class="fi-pencil"></i> 
         </a> 
        </td> 
       </tr> 
       {% endfor %} 

      </tbody> 

感謝。

答えて

0

これらは、必要なものによって3つの異なるオプションです。ただそれらの1つを使用してください。

例では、ログインしているユーザーが最初に表示されます。それはリストの前に置くだけのアイテムです。あなたが特定の順序でリストを取得したいのであれば、その最も簡単で、

<ul> 
<li>{{ user.username }}</a></li> 
{% for item in users %} 
    {% if item != user %} 
    <li>{{ item.username }}</a></li> 
    {% endif %} 
{% endfor %} 
</ul> 

ただし、テンプレートにその権利を行うには、それを達成するための2つの方法があります。順番はこの1つのListViewのためだけであれば、しかしthe get_queryset() method of ListView()

class PListView(ListView): 
    model = Product 
    template_name = "app/product_list.html" 

    get_queryset(self): 
     return Product.objects.all().order_by('name') 

を使用したデータは常には、製品モデルのすべてのリストに、この方法で注文する必要がある場合は、次のことができorder it right on the model代わり

class Product(models.Model): 
    name = models.CharField(max_length=50) 

    class Meta: 
     ordering = ['name', ] 
関連する問題