python 3.4.2 django 1.7.1 postgres 9.4
我正在尝试从postgres查询数据并将其发送到模板进行渲染。
我已经包含了模型,视图,urls.py和媒体页面
我认为问题出在views.py中,而contextDict var发送给模板。
我认为问题是什么 步骤3-6是f **** up,我认为问题出在我对DB的查询,或者我如何将数据传递给模板
模型:
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 :(得分:0)
发现问题
我正在查询Project表而不是Media表
当用户点击index.html并单击媒体链接时,我上面发送的代码会向Project表创建一个SQL,并且由于后续行基于第一个变量的初始值,错误会从那里级联。