miracle2k / django-tables2

django-tables2 - An app for creating HTML tables
Other
60 stars 20 forks source link

Template synthax error when using ona model with ForeignKeys #5

Open CalBR opened 14 years ago

CalBR commented 14 years ago

Hi

Everytime I try to use django-table on a model with foreignkey I receive an error message saying "global name 'name' is not defined". It does work as expected on the other models.

Environment:

Request Method: GET Request URL: http://localhost:8000/ibama/list_mp/ Django Version: 1.1.1 Python Version: 2.6.4 Installed Applications: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.admin', 'prods.ibama'] Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware')

Template error: In template e:\django\prods\templates\list_mp.html, error at line 11 Caught an exception while rendering: global name 'name' is not defined 1 : 

2 : 3 : 4 : 5 : {% for coluna in table.columns %} 6 : 7 : {% endfor %} 8 : 9 : 10 : 11 : {% for linha in table.rows %} 12 : 13 : {% for valor in linha %} 14 : 15 : {% endfor %} 16 : 17 : {% endfor %} 18 : 19 :
{{ coluna }}
{{ valor }}

Traceback: File "E:\Django\Python26\lib\site-packages\django\core\handlers\base.py" in get_response

  1. response = callback(request, _callback_args, *_callback_kwargs) File "E:\Django\prods\ibama\views.py" in list_mp
  2. return render_to_response('list_mp.html', {'table': mats}) File "E:\Django\Python26\lib\site-packages\django\shortcutsinit.py" in render_to_response
  3. return HttpResponse(loader.render_to_string(_args, *_kwargs), **httpresponse_kwargs) File "E:\Django\Python26\lib\site-packages\django\template\loader.py" in render_to_string
  4. return t.render(context_instance) File "E:\Django\Python26\lib\site-packages\django\templateinit.py" in render
  5. return self.nodelist.render(context) File "E:\Django\Python26\lib\site-packages\django\templateinit.py" in render
  6. bits.append(self.render_node(node, context)) File "E:\Django\Python26\lib\site-packages\django\template\debug.py" in render_node
  7. raise wrapped

Exception Type: TemplateSyntaxError at /ibama/list_mp/ Exception Value: Caught an exception while rendering: global name 'name' is not defined

miracle2k commented 14 years ago

I don't think there is enough information here for me to analyze the problem. Can you post the stack trace of the "wrapped exception"?

Alternatively, the relevant pieces of code from your model/table/template might be helpful.

CalBR commented 14 years ago

I don´t know what is the wrapped exception so, I´ll give you the other information but I think my files are pretty normal. The only not usual thing is that my foreign keys points to regular fields (using the to_field) and not id on other models.

model.py

from django.db import models

class Prodlist(models.Model): codigo = models.IntegerField('Codigo NCM', unique = True) descricao = models.CharField('Descricao', max_length = 200) unidade = models.CharField('Unidade', max_length = 2, blank = True)

def __unicode__(self):
    return self.descricao

class Familia(models.Model): codigo = models.IntegerField('Familia#', unique = True) parent = models.ForeignKey('self', to_field = 'codigo', blank=True, null = True, related_name = 'child_set') divisao = models.CharField('Divisao', max_length = 10) marca = models.CharField('Marca', max_length = 10) descricao = models.CharField('Descricao', max_length = 50)

def __unicode__(self):
    return self.descricao

class Produto(models.Model): codigo = models.IntegerField('Codigo', unique = True) descricao = models.CharField('Codigo', max_length = 35) unidade = models.CharField('Unidade', max_length = 2, choices = UNIDADES_VALIDAS) familia = models.ForeignKey('Familia', to_field = 'codigo') prodlist = models.ForeignKey('Prodlist', to_field = 'codigo')

def __unicode__(self):
    return self.descricao

views.py

from django.shortcuts import render_to_response from prods.ibama.models import Familia, Produto, Prodlist import django_tables as tables

class ModeloTable(tables.ModelTable):

class Meta:
    model = Produto

def list_mp(request): qs = Produto.objects.all() mats = ModeloTable(qs) return render_to_response('list_mp.html', {'table': mats})

list_mp.html

{% for coluna in table.columns %} {% endfor %} {% for linha in table.rows %} {% for valor in linha %} {% endfor %} {% endfor %}
{{ coluna }}
{{ valor }}

Template error header listing

TemplateSyntaxError at /ibama/list_mp/

Caught an exception while rendering: global name 'name' is not defined

Request Method: GET Request URL: http://localhost:8000/ibama/list_mp/ Exception Type: TemplateSyntaxError Exception Value:

Caught an exception while rendering: global name 'name' is not defined

Exception Location: E:\Django\Python26\lib\site-packages\django\template\debug.py in render_node, line 81 Python Executable: E:\Django\Python26\python.exe Python Version: 2.6.4 Python Path: ['E:\Django\prods', 'E:\Django\~dp0\Python26\Lib\site-packages', 'E:\Django\Python26\python26.zip', 'E:\Django\Python26\DLLs', 'E:\Django\Python26\lib', 'E:\Django\Python26\lib\plat-win', 'E:\Django\Python26\lib\lib-tk', 'E:\Django\Python26', 'E:\Django\Python26\lib\site-packages', 'E:\Django\Python26\lib\site-packages\PIL'] Server time: Sab, 21 Ago 2010 21:56:38 -0300

miracle2k commented 14 years ago

Try the latest head. I fixed a NameError there, presumably the one you're running into. The bad news is that this NameError occured while trying to display an exception, so you'll probably be seeing another error; one that should tell us more about what is wrong, though.

CalBR commented 14 years ago

You are right. I´ve download and installed the latest version and executed my app using the same data and the error has changed, although pointing to the same problem: a foreign key which point to a "to_field" and not to the other model´s id.

The error now says: "Caught an exception while rendering: Could not resolve familia from familia".

"familia", as you may see above, is a foreign key in the model Produto. Everything else remains the same.

CalBR commented 14 years ago

I found what was causing the error: there were some foreign keys with invalid values. I´ve corrected them and django-tables worked as expected.

It looks like it´s not managing correctly database errors when they happen in an associated model.