使用django显示视图中对象的属性

时间:2016-03-08 21:05:46

标签: python django

我正在使用Django 1.9。我想显示一个视图,其中包含保存在数据库中的特定车辆的属性。但是当我尝试访问视图时,我会收到Attribute error而不是车辆的特征列表。我应该纠正什么才能很好地显示每辆车的视图?每辆车都有ID,而我的数据库中只有一辆车(所以我将该车辆的ID放在我的网址中)。提前致谢

views.py
===========================
    def getAuto(request, idAuto):
        auto = Auto.objects.get(idAuto=idAuto)
        ctx = {"auto": auto}
        return render('vehiculo_especifico.html', ctx)


urls.py
==========================
    url(r'^verAuto/(?P<idAuto>\d+)/$', views.verAuto),  


models.py
==========================
    class Auto(models.Model):
        idAuto = models.AutoField(primary_key=True)
        Marca = models.CharField(max_length=45)
        Modelo = models.CharField(max_length=45)
        VarianteModelo = models.CharField(max_length=45)
        Ano = models.IntegerField(null=False, default=2016)
        etc..

Traceback (IOError):
=========================
Traceback:

File "C:\Users\User\Envs\vehilometro\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "C:\Users\User\Envs\vehilometro\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\User\Envs\vehilometro\vehilometro\vehiapp\views.py" in verAuto
  88.     return render('vehiculo_especifico.html', ctx)

File "C:\Users\User\Envs\vehilometro\lib\site-packages\django\shortcuts.py" in render
  67.             template_name, context, request=request, using=using)

File "C:\Users\User\Envs\vehilometro\lib\site-packages\django\template\loader.py" in render_to_string
  96.             template = get_template(template_name, using=using)

File "C:\Users\User\Envs\vehilometro\lib\site-packages\django\template\loader.py" in get_template
  32.                 return engine.get_template(template_name, dirs)

File "C:\Users\User\Envs\vehilometro\lib\site-packages\django\template\backends\django.py" in get_template
  40.             return Template(self.engine.get_template(template_name, dirs), self)

File "C:\Users\User\Envs\vehilometro\lib\site-packages\django\template\engine.py" in get_template
  190.         template, origin = self.find_template(template_name, dirs)

File "C:\Users\User\Envs\vehilometro\lib\site-packages\django\template\engine.py" in find_template
  157.                         name, template_dirs=dirs, skip=skip,

File "C:\Users\User\Envs\vehilometro\lib\site-packages\django\template\loaders\base.py" in get_template
  40.                 contents = self.get_contents(origin)

File "C:\Users\User\Envs\vehilometro\lib\site-packages\django\template\loaders\filesystem.py" in get_contents
  24.             with io.open(origin.name,  encoding=self.engine.file_charset) as fp:

Exception Type: IOError at /verAuto/1/
Exception Value: [Errno 22] Invalid argument: u"C:\\vehiapp\\Templates\\{'auto': <Auto: Nissan 370z Touring AT>}"

1 个答案:

答案 0 :(得分:1)

<强> urls.py

url(r'^getAuto/(?P<idAuto>\d+)/$', views.verAuto),

<强> views.py

def verAuto(request, idAuto):
    auto = Auto.objects.get(idAuto=idAuto)
    ctx = {
    "auto":auto
    }
    return render(request,'vehiculo_especifico.html', ctx)

<强> vehiculo_especifico.html

...
Marca: {{auto.Marca}}
Modelo: {{auto.Modelo}}
...

阅读PEP(Python代码样式指南) https://www.python.org/dev/peps/pep-0008/