2016-03-08 61 views
0

我正在使用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>}" 
+0

您能否包含完整的追溯?它應該指向有問題的聲明 – DaveBensonPhillips

+0

您的視圖名爲'getAuto',但在urls中將其稱爲'verAuto',這會導致報告的錯誤,因爲'views'模塊沒有屬性'verAuto' – schwobaseggl

+0

已更正一些事情,但現在我得到一個IOError。我現在會發布回溯 – KPavezC

回答

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/

+0

我做到了,現在它顯示IOError,我現在會發布回溯,謝謝 – KPavezC

+0

render需要請求對象。 'render(request,'template.html',ctx)' – erajuan

+0

非常感謝!我知道我忘了一些東西 – KPavezC

相關問題