我正在使用Django 1.9。我想顯示一個包含保存在數據庫中的特定車輛屬性的視圖。但是,當我嘗試訪問視圖時,我收到屬性錯誤而不是車輛特徵列表。我應該修正哪些數據,以便正確顯示每輛車的視圖?每輛車都有一個身份證,而我的數據庫中只有一輛車(所以我將該車的身份證放入我的網址中)。在此先感謝使用Django顯示視圖中的對象的屬性
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..
回溯(IO錯誤):
回溯:
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>}"
您能否包含完整的追溯?它應該指向有問題的聲明 – DaveBensonPhillips
您的視圖名爲'getAuto',但在urls中將其稱爲'verAuto',這會導致報告的錯誤,因爲'views'模塊沒有屬性'verAuto' – schwobaseggl
已更正一些事情,但現在我得到一個IOError。我現在會發布回溯 – KPavezC