0

I'm trying to use the django tables 2 tutorial (Link) and I'm stuck with this error.

Edit: this happens when I try to access 127.0.0.1:8000/tables/

I'm stuck at the this part in the tutorial: "Hook the view up in your URLs, and load the page, you should see:" It doesn’t display this table and instead shows the error displayed below.

I tried the solutions listed in the other questions, but it hasn't helped. Can someone help me please?

Here's the code: https://github.com/karbfg10k/temp-work/tree/master/IMedMonitor/IMed

And here's the error

Unhandled exception in thread started by <function wrapper at 0x7fa9216456e0>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 116, in inner_run
    self.check(display_num_errors=True)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 10, in check_url_config
    return check_resolver(resolver)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 19, in check_resolver
    for pattern in resolver.url_patterns:
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/karthik/Code/MedicalDevices/IMedMonitor/IMed/IMed/urls.py", line 20, in <module>
    url(r'^tables/', include('tables.urls')),
  File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/karthik/Code/MedicalDevices/IMedMonitor/IMed/tables/urls.py", line 6, in <module>
    url(r'^$', views.index, name='index'),
AttributeError: 'module' object has no attribute 'index'
2
  • 2
    You have index method commented out Commented Jan 17, 2016 at 23:45
  • Thanks, but how if I uncomment it, it just displays the HttpResponse, how do I get it to display the tables instead? Commented Jan 17, 2016 at 23:57

1 Answer 1

1

Uncomment the index method as Joel has stated and then render an html page as you have done in the people method, passing the table data as well.

https://docs.djangoproject.com/en/1.9/intro/tutorial03/

About 3/4 down the page (slightly modified to suit your example):

def index(request):
    table_object = ......
    template = loader.get_template('correct_page_here')
    context = {
        'table_obj': table_object,
}
    return HttpResponse(template.render(context, request))

In the corresponding html page add the appropriate tags to render the table

https://django-tables2.readthedocs.org/en/latest/pages/template-tags.html

I changed your model files to this:

from __future__ import unicode_literals

from django.db import models

data = [
    {"name": "Me!"},
    {"name": "Myself!"},
]

# Create your models here.
class Person(models.Model):
    name = models.CharField(verbose_name="full name", max_length = 20)

After changing the model file make sure you run

python manage.py makemigrations && python manage.py migrate

Your tables/urls.py file to:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^people/$', views.people, name='people'),
]

Your views.py file to:

from django.shortcuts import render
from django.http import HttpResponse
from models import Person 


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")


def people(request):
    return render(request, "people.html", {"people": Person.objects.all()})
    table = Person(data)

The installed apps in IMed/IMed/settings.py to:

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_tables2',
    'IMed',
    'tables',
]

Now if you run the server and go here:

http://127.0.0.1:8000/tables/people/

The people view will work and you can copy the same process in index as you have in people.

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, I'm new to django, so I still have a couple of questions. I changed the 'correct_page_here' to tables/people.html. what do I do about the table_object = bit ? Also I've already put {% load render_table from django_tables2 %} in the html page, so I shouldn't have to add the tags right?
Hi, thanks for the help!, I'm able to get the people page running but the table, doesn’t seem to render anything. Only ID and full name are displayed as text, no table is displayed. Any ideas why this is? I've updated the github with the code
Hi, is there any data in your table? You can confirm by putting a breakpoint in the people method in views and seeing what is stored in Person.objects.all()
I used the shell and you're right it's empty, I added a name and it prints it, but again just as text with no table
Good stuff!!!!! If this answered your question I'd appreciate it if you would accept the answer ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.