Django类型对象'Template'没有属性'objects'?

时间:2018-09-05 08:34:47

标签: django django-templates django-views

我正在使用Django 1.10.5。当我点击时出现此错误 / rentacar / list /

中的AttributeError

观看次数

进口

from rentacar.forms import *
from template.models import Template
from module.models import *

@csrf_protect
def rentacar_list(request, page_number=1):
    menu_config_list = MenuItemRentacarList.objects.all()[0]
    menu_config = MenuItemRentacarList.objects.get(id=menu_config_list.id)

    all_cars = Car.objects.all().order_by('-id')
    if menu_config.menu_item_rentacar_list_show_unavailable == 0:
        all_cars = all_cars.exclude(car_available=0)
    else:
        all_cars = all_cars

    cars_page = Paginator(all_cars, menu_config.menu_item_rentacar_list_pagination)
    args['cars'] = cars_page.page(page_number)
    template = Template.objects.get(template_default__exact=1)
    args['main_menu'] = MenuMenu.objects.get(id__exact=template.template_main_menu_id)
    args['menu_items'] = MenuItem.objects.filter(
        menu_item_menu=args['main_menu'],
        menu_item_published=1,
    )
    template_page = template.template_alias + str("/rentacar/rentacar_cars_list.html")
    args['current_menu_item'] = menu_config.menu_item_rentacar_list_menu_item
    all_modules = Module.objects.filter(
        module_show_option__exact='all',
        module_published=1
    )
    selected_modules = Module.objects.filter(
        module_show_option__exact='selected',
        module_published=1,
        module_menu_item=args['current_menu_item']
    )
    excluded_modules = Module.objects.filter(
        module_show_option__exact='except',
        module_published=1,
    ).exclude(
        module_menu_item=args['current_menu_item']
    )
    args['modules'] = list(chain(all_modules, selected_modules, excluded_modules))
    return render(request, template_page, args)

网址

url(r'^rentacar/list/$', extension_views.rentacar_list),

我正在使用模板作为模型并定义模板对象,然后从我的模板应用程序中获取它。是否有任何简便的方法来呈现我的模板。

异常位置:rentacar_list中的D:\ buggy \ extension \ views.py,第766行

模板模型

from mega_admin.models import MenuMenu



class Template(models.Model):
    class Meta():
        db_table = "template"
        verbose_name = "Template"
        verbose_name_plural = "Templates"

    template_title = models.CharField(
        verbose_name="Template title",
        max_length=200
    )
    template_alias = models.CharField(
        verbose_name="Template alias",
        max_length=200
    )
    template_description = models.TextField(
        verbose_name="Template description",
        blank=True,
    )
    template_version = models.IntegerField(
        verbose_name="Template version",
        null=True,
        blank=True,
    )
    template_default = models.BooleanField(
        default=0,
        blank=False,
        null=False
    )
    template_main_menu = models.ForeignKey(
        MenuMenu,
        blank=True,
        null=True,
        on_delete=models.CASCADE,
    )

    def __unicode__(self):
        return u'%s' % self.template_title + str(" v") + str(self.template_version)

class TemplatePosition(models.Model):
    class Meta():
        db_table = "template_position"
        verbose_name = "Template position"
        verbose_name_plural = "Template positions"

    template_position_template = models.ForeignKey(
        'template.Template',
        related_name="template_position_template_key",
        verbose_name="Template positions template",
        blank=False,
        null=False,
        on_delete=models.CASCADE,
    )
    template_position_name = models.CharField(
        verbose_name="Template position name",
        max_length=200,
        blank=False,
        null=False,
    )
    template_position_alias = models.CharField(
        verbose_name="Template position alias",
        max_length=200,
        blank=False,
        null=False,
    )

    # def __unicode__(self):
    #   return u'%s' % self.template_position_name

跟踪

File "D:\projects\buggy\venv\lib\site-packages\django\core\handlers\exception.py" in inner
  39.             response = get_response(request)

File "D:\projects\buggy\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "D:\projects\buggy\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "D:\projects\buggy\venv\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "D:\buggy\extension\views.py" in rentacar_list
  766.     template = Template.objects.get(template_default__exact=1)

Exception Type: AttributeError at /rentacar/list/
Exception Value: type object 'Template' has no attribute 'objects'

1 个答案:

答案 0 :(得分:1)

此错误即将到来,因为我正在导入我的客户模型模板并内置在模板中

from template.models import Template
from django.template import Context, Template
def func1(request):

  template = **Template**.objects.get(template_default__exact=1)

def func2(request):            
      template = **Template** ("""link{{ request.get_host }}{% url 'detail' driver_id=driver_id %}""")

相同的模板名称,但具有不同的导入。一个是内置导入,第二个是模型导入,这就是为什么发生属性错误的原因。

特别感谢:

威廉·范·昂塞姆

阿拉斯代尔

丹尼尔·罗斯曼

这些伟人解决了这个答案。