未定义基于 Django class 的视图查询集模型
Django class based views queryset model isn't defined
我正在 Django
制作一个网络应用程序来存储和分类课程。我遇到的问题是查询集无法识别模型。我已经设法将所有内容都拉入基于 class 的视图中并弄乱了数据,但是,当我尝试执行查询集时,它只是说模型 "Course" 未定义。我导入了以下模型:
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')
我有以下基于 class 的观点。像 date_screen()
这样的函数是在另一个文件中编写并导入的,它们在我基于函数的视图中工作。问题是它一直说 Course
未定义。如果您发现基于 class 的视图有任何其他问题,请提醒我。我可以开发一个基于 class 的视图来提取所有数据,但目前任何更多的细微差别都是令人头晕的。
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
您是否从 models.py 导入了模型?
您应该通过以下方式导入模型,
from .models import Course
我不确定到底是什么问题,但看起来你把一些东西放在了不合适的地方。
# URLs
url(
r'^local/(?P<location>[-\w]+)/$',
views.Courses_By_Location.as_view(),
name='by_location',
),
# Views
class Courses_By_Location(ListView):
model = Course
context_object_name = 'courses'
def dispatch(self, request, *args, **kwargs):
self.location = kwargs.get('location', 'DEFAULT-LOCATION')
return super().dispatch(request, *args, **kwargs)
def get_queryset(self):
# `date_screen` must return a QuerySet
return date_screen(
# assuming the `Gen_Location` model has a `name` field
super().get_queryset().filter(location__name__iexact=self.location),
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['locations'] = location_screen(self.object_list)
context['categories'] = category_screen(self.object_list)
context['count'] = self.object_list.count()
return context
我正在 Django
制作一个网络应用程序来存储和分类课程。我遇到的问题是查询集无法识别模型。我已经设法将所有内容都拉入基于 class 的视图中并弄乱了数据,但是,当我尝试执行查询集时,它只是说模型 "Course" 未定义。我导入了以下模型:
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')
我有以下基于 class 的观点。像 date_screen()
这样的函数是在另一个文件中编写并导入的,它们在我基于函数的视图中工作。问题是它一直说 Course
未定义。如果您发现基于 class 的视图有任何其他问题,请提醒我。我可以开发一个基于 class 的视图来提取所有数据,但目前任何更多的细微差别都是令人头晕的。
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
您是否从 models.py 导入了模型?
您应该通过以下方式导入模型,
from .models import Course
我不确定到底是什么问题,但看起来你把一些东西放在了不合适的地方。
# URLs
url(
r'^local/(?P<location>[-\w]+)/$',
views.Courses_By_Location.as_view(),
name='by_location',
),
# Views
class Courses_By_Location(ListView):
model = Course
context_object_name = 'courses'
def dispatch(self, request, *args, **kwargs):
self.location = kwargs.get('location', 'DEFAULT-LOCATION')
return super().dispatch(request, *args, **kwargs)
def get_queryset(self):
# `date_screen` must return a QuerySet
return date_screen(
# assuming the `Gen_Location` model has a `name` field
super().get_queryset().filter(location__name__iexact=self.location),
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['locations'] = location_screen(self.object_list)
context['categories'] = category_screen(self.object_list)
context['count'] = self.object_list.count()
return context