2016-01-28 74 views
2

我是Django的新手。我正嘗試使用模板在我的索引視圖中顯示來自我的項目模型的數據。我盡我最大努力來構建這個類似於民意調查應用程序的應用程序。我不知道我做錯了什麼。我使用python 2.7和Django的1.8.6如何在Django模板中顯示模型數據

這裏是我的網址:

from django.conf.urls import url 

from . import views 

app_name = 'project' 
urlpatterns = [ 
    url(r'^$', views.IndexView.as_view(), name='index'), 
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'), 
] 

這裏是我的模型:

import datetime 
from django.db import models 
from django.utils.encoding import python_2_unicode_compatible 
from django.contrib.auth.models import User 
from django.utils import timezone 


@python_2_unicode_compatible # only if you need to support Python 2 
class Contractor(models.Model): 
    #project 
    name = models.CharField(max_length=50) 
    address = models.CharField(max_length=100, blank=True) 
    phone = models.CharField(max_length=14, blank=True) 
    city = models.CharField(max_length=60, blank=True) 
    state = models.CharField(max_length=2, blank=True) 
    created_by = models.ForeignKey(User, related_name='Contractor_created_by') 
    created_date = models.DateTimeField() 
    modified_by = models.ForeignKey(User, related_name='Contractor_modified_by') 
    modified_date = models.DateTimeField() 

    def __str__(self): 
     return self.name 

@python_2_unicode_compatible # only if you need to support Python 2 
class Project(models.Model): 
    name = models.CharField(max_length=50) 
    jobNumber = models.CharField(max_length=8) 
    shopOut = models.DateTimeField(null=True) 
    shopIn = models.DateTimeField(null=True) 
    delivery = models.DateTimeField(null=True) 
    job1 = models.CharField(max_length=50, null=True) 
    job2 = models.CharField(max_length=50, null=True) 
    job3 = models.CharField(max_length=50, null=True) 
    contractor = models.ForeignKey(Contractor, on_delete=models.CASCADE, default=101) 
    created_by = models.ForeignKey(User, related_name='Project_created_by') 
    created_date = models.DateTimeField(auto_now_add=True) 
    modified_by = models.ForeignKey(User, related_name='Project_modified_by') 
    modified_date = models.DateTimeField(auto_now=True) 
    def __str__(self): 
     return self.name 
    def save(self, *args, **kwargs): 
     if not self.id: 
      self.created_by = User.objects.get(id=1) 
      self.modified_by = User.objects.get(id=1) 
      super(Project, self).save(*args, **kwargs) 
      year = datetime.datetime.now().year 
      self.jobNumber = '{}{:04d}'.format(year, self.id) 
     self.modified_by = User.objects.get(id=1) 
     super(Project, self).save(*args, **kwargs) 

這是我的看法:

from django.shortcuts import get_object_or_404, render 
from django.http import HttpResponseRedirect 
from django.core.urlresolvers import reverse 
from django.views import generic 
from django.utils import timezone 

from .models import Project 

# Create your views here. 
class IndexView(generic.ListView): 
    model = Project 
    template_name = 'project/index.html' 

    def get_queryset(self): 
     return Project.objects 

class DetailView(generic.DetailView): 
    model = Project 

這是我的模板:

{% load staticfiles %} 
<h1>Projects</h1> 
<ul> 
{% for projects in project.get_queryset %} 
    in for loop 
    <!-- <li><a href="{% url 'projects:detail' projects.id %}">{{ projects.name }}</a></li> --> 
    <li><a href="#">Test</a></li> 
{% endfor %} 

</ul> 
end of list 

當我去的網頁,我收到了H1項目,空UL,線,上面寫着「名單的終結」

回答

1

在你get_queryset,你應該返回Project.objects.all()

在模板中,你不需要做project.get_queryset,該get_queryset方法被調用爲您和值傳遞給模板object_list<objectname>_list,以及其他參數。在你的情況下,對象是Project,所以應該有project_list變量以及object_list

你可以這樣做:

{% for project in project_list %} 
    <li><a href="{% url 'projects:detail' project.id %}">{{ project.name }}</a></li> 
{% endfor %} 

或者:

{% for project in object_list %} 
    <li><a href="{% url 'projects:detail' project.id %}">{{ project.name }}</a></li> 
{% endfor %} 

你可以閱讀更多關於它在這裏:https://docs.djangoproject.com/en/1.9/ref/class-based-views/generic-display/#listview

+0

你至少可以解釋爲什麼運應返回的查詢集,。 。我沒有檢查看看你是否錯過了Shang Wangs回答 – Sayse

+0

除了Shang Wang的回答,我解釋了queryset是如何在ListView中處理的以及兩種可能的方式遍歷對象列表。 – masnun

+0

謝謝!我希望兩天前我會問。 – user908759

0

你可能也想嘗試:

{% for project in object_list %} 
    <li><a href="{% url 'projects:detail' project.id %}">{{ project.name }}</a></li> 
{% endfor %} 

object_list是如果您使用ListView,則可以通過在視圖中定義context_object_name來更改該默認名稱。這裏是django doc about that

0

你的查詢集不會在一分鐘內返回一個查詢集,目前它只是返回一個相關的經理。你應該讓返回的查詢集...

def get_queryset(self): 
    return Project.objects.all() # added all 
相關問題