2015-01-03 71 views
1

蟒蛇3.4.2 的Django 1.7.1 的Postgres 9.4的Django 1.7,模板不是從views.py

顯示數據我試圖從Postgres的查詢數據並將其發送到模板進行渲染。

我已經包括了模型,視圖,urls.py,和媒體頁面

我認爲這個問題是在views.py,和contextDict VAR我發送到模板。

  1. 什麼應該發生:
    1. 用戶點擊index.html,然後點擊一個鏈接,查看有關
    2. 的index.html加載views.py「媒體」功能的媒體資產的詳細信息
    3. 媒體函數捕獲slugify URL並使用它來查詢DB
    4. views.py中的'media'函數將數據加載到變量中
    5. 'media'函數傳遞請求,模板url和變量t Ø模板
    6. 模板處理變量並將該頁面發送到用戶的客戶端
  2. 發生了什麼
    1. 用戶點擊index.html,然後點擊鏈接查看有關媒體資產的詳細資訊
    2. 的index.html加載views.py MDIA功能
    3. 用戶將看到一個渲染頁面的文本「指定的項目{{projectSlugTitle}}不存在」

什麼,我認爲這個問題是 步驟3-6爲f ****了,我覺得問題在於無論是在我的查詢到數據庫,或者我如何傳遞數據給模板

模型:

from django.db import models 
from django.utils import timezone 
from django.template.defaultfilters import slugify 

#table for media files: audio, video, design 
class Media(models.Model): 
    #choicesConstants 
    #type 
    MEDIATYPE_FILM = 'MEDIATYPE_FILM' 
    MEDIATYPE_AUDIO = 'MEDIATYPE_AUDIO' 
    MEDIATYPE_DESIGN = 'MEDIATYPE_DESIGN' 

    #category 
    MEDIACATEGORY_MAJOR = 'MEDIACATEGORY_MAJOR' 
    MEDIACATEGORY_INDIE = 'MEDIACATEGORY_INDIE' 

    #genre 
    MEDIAGENRE_RAP = 'MEDIAGENRE_RAP' 
    MEDIAGENRE_ROCK = 'MEDIAGENRE_ROCK' 
    MEDIAGENRE_TECHNO = 'MEDIAGENRE_TECHNO' 

    #choicesList 
    choicesType = (
     (MEDIATYPE_FILM,'Video'), 
     (MEDIATYPE_AUDIO,'Audio'), 
     (MEDIATYPE_DESIGN,'Design'), 
    ) 

    choicesCategory = (
     (MEDIACATEGORY_INDIE,'Indie'), 
     (MEDIACATEGORY_MAJOR,'Major'), 
    ) 

    choicesGenre = (
     (MEDIAGENRE_RAP,'Rap'), 
     (MEDIAGENRE_ROCK,'Rock'), 
     (MEDIAGENRE_TECHNO,'Techno') 
    ) 

    #boolean 
    mediaPublished = models.BooleanField(default=True) 

    #char fields 
    title = models.CharField(max_length=256,blank=True) 
    type = models.CharField(max_length=256,choices=choicesType, default=MEDIATYPE_FILM) 
    category = models.CharField(max_length=256,choices=choicesCategory,default=MEDIACATEGORY_MAJOR) 
    genre = models.CharField(max_length=256,choices=choicesGenre,default=MEDIAGENRE_TECHNO) 

    #integer fields 
    views = models.IntegerField(default=0) 
    upVotes = models.IntegerField(default=0) 
    downVotes = models.IntegerField(default=0) 

    #date fields 
    dateAdded = models.DateTimeField(default=timezone.now) 
    datePublished = models.DateTimeField(blank=True,null=True) 
    dateDePublished = models.DateTimeField(blank=True,null=True) 

    #urlfields 
    intUrl = models.URLField(blank=True) 
    extUrl = models.URLField(blank=True) 

    #email fields 
    mediaEmail = models.EmailField(max_length=254,blank=True) 

    #decimalFields 
    mediaB2bPrice = models.DecimalField(max_digits=20,decimal_places=2,default=0) 
    mediaB2cPrice = models.DecimalField(max_digits=20,decimal_places=2,default=0) 

    #slugFields 
    slug1 = models.SlugField() 

    #functionUtility 
    def __str__(self): 
     return self.title 

    #functionMath 
    def totalVotes(self): 
     return int(self.upVotes)+int(self.downVotes) 

    def percentUpVotes(self): 
     return int(self.upVotes)/int(self.totalVotes) 

    def percentDownVotes(self): 
     return int(self.downVotes)/int(self.totalVotes) 

    def save(self, *args,**kwargs): 
     self.slug1 = slugify(self.title) 
     super(Media, self).save(*args, **kwargs) 

    #metaData 
    class Meta: 
     ordering = ['dateAdded','title'] 
     get_latest_by = 'dateAdded' 
     verbose_name = 'Media' 
     verbose_name_plural = 'Media' 

#tablef for projects, contain multiple media files 
class Project(models.Model): 
    #manyToMany relationships 
    media = models.ManyToManyField(Media,null=True,blank=True) 

    #boolean 
    projectPublished = models.BooleanField(default=True) 

    #charFields 
    title = models.CharField(blank=True,max_length=256) 

    #textFields 
    projectDescription = models.TextField(blank=True) 

    #email fields 
    projectEmail = models.EmailField(max_length=254,blank=True) 

    #dateFields 
    dateCreated = models.DateTimeField(default=timezone.now) 
    datePublished = models.DateTimeField(blank=True,null=True) 

    #decimalFields 
    projectB2bPrice = models.DecimalField(max_digits=20,decimal_places=2,default=0) 
    projectB2cPrice = models.DecimalField(max_digits=20,decimal_places=2,default=0) 

    #slugFields 
    slug1 = models.SlugField() 

    #functionsUtility 
    def __str__(self): 
     return self.title 

    def save(self, *args, **kwargs): 
     self.slug1 = slugify(self.title) 
     super(Project, self).save(*args, **kwargs) 

    #metaData 
    class Meta: 
     ordering = ['dateCreated','title'] 
     get_latest_by = 'dateAdded' 
     verbose_name = 'Project' 
     verbose_name_plural = 'Projects' 

視圖:

def project(request,theProjectSlug): 
    contextDict = {} 
    try: 
     #retrieve the project with the matching slug name 
     project = Project.objects.get(slug1=theProjectSlug) 
     contextDict['projectName'] = project.title 

     #retrieve all of the associated media files of the project above 
     mediaFiles = Media.objects.all().filter(project=project) 

     #add mediaFiles to contextDict,add the project to the contextDict 
     contextDict['mediaFilesOfProject'] = mediaFiles 
     contextDict['project'] = project 

    except Project.DoesNotExist: 
     pass 

    return render(request, 'famecity/media.html', contextDict) 

urls.py:

urlpatterns = patterns('', 
         url(r'^$',views.index,name='index'), 
         url(r'^about/',views.about,name='about'), 
         url(r'^media/(?P<theProjectSlug>[\w\-]+)/$',views.project,name='project') 

      ) 

所呈現的頁面:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Fame.city Projects 
    </title> 
</head> 

<body> 
    <h1> 
     projectName: {{ projectName }}<br/> 
     mediaFilesOfProject: {{mediaFilesOfProject}}<br/> 
     project: {{project}}<br/> 
    </h1> 
    {% if project %} 
     {% if media %} 
      <ul> 
       {% for mFile in media %} 
        <li> 
         <a href="{{mFile.url}}">{{mFile.title}}</a> 
        </li> 
       {% endfor %} 
      </ul> 
     {% else %} 
      <strong>No media files exist</strong> 
     {% endif %} 
    {% else %} 
     The specified project {{projectSlugTitle}} does not exist 
    {% endif %} 
</body> 
</html> 
+0

嘿。我的問題與這個問題無關。我剛到這裏尋找別的東西。我只是想知道爲什麼你使用基於函數的視圖,而Django文檔和所有最新的教程建議使用基於類的通用視圖。我開始構建一個Web應用程序,您是否會推薦長期使用基於功能的視圖? –

回答

0

發現問題

我查詢項目表,而不是媒體表

當用戶點擊index.html,然後單擊媒體鏈接,我上面的代碼發送給Project表創建一個SQL,並且由於後面的行基於第一個變量的初始值,從那裏級聯錯誤。