我生產一個web應用程序。我遇到的問題是查詢集不能識別模型。我已經成功地將所有的基於類的視圖和數據混淆了,但是,當我嘗試並執行查詢設置時,它只是說模型「課程」未定義。我導入了以下型號:Django的類查詢集模型視圖中<code>Django</code>存儲和排序的課程沒有定義
class Course(models.Model):
provider = models.ForeignKey(Provider)
title = models.CharField('Course Title',
max_length=200,
)
first_line = models.CharField('Address Line: 1',
max_length=200,
)
second_line = models.CharField('Address Line: 2',
max_length=200,
)
third_line = models.CharField('Address Line: 3',
max_length=200,
)
city = models.CharField('City',
max_length=200,
)
post_code = models.CharField('Post Code',
max_length=200,
)
course_description = models.TextField('Description')
date = models.DateField('Date')
start_time = models.TimeField('Starting time')
finish_time = models.TimeField('Finishing time')
duration = models.IntegerField('Number of hours')
CPD = models.IntegerField('CPD points')
link = models.CharField('Link', max_length=200)
category = models.ForeignKey(Categories)
gen_cat = models.ForeignKey(Gen_Categories)
location = models.ForeignKey(Gen_Location)
cost = models.FloatField('Cost')
而且我有以下基於類的視圖。像date_screen()
那樣的函數被寫入另一個文件並被導入,它們在我的基於函數的視圖中工作。問題是它一直說Course
沒有定義。如果你可以發現基於課堂觀點的其他問題,請給我一個提醒。我可以開發一個基於班級的視圖,可以提取所有數據,但更多的細微差別現在是頭腦風暴。
class Courses_By_Location(ListView):
template_name = 'courses/course_list.html'
model = Course
def get_queryset(self):
self.Course = get_object_or_404(Course, name=self.args[0].order_by('date'))
raw_courses = Course.objects.filter(location=self.location)
courses = date_screen(raw_courses)
categories = category_screen(courses)
locations = location_screen(courses)
def get_context_data(self, **kwargs):
context = super(searchView, self).get_context_data(**kwargs)
context.update({'locations': self.locations,
'categories': self.categories,
'courses': self.courses,
'count': self.count,})
return context
如果您可以發佈基於工作功能的視圖,則更有可能找到解決方案。 – Matt
感謝您的輸入。我已經採取了另一個答案,因爲他已經完全重寫了這個觀點,並且工作得很好,而且非常清楚。再次感謝您抽出時間幫助我。 – max89