django 表 'module' 对象没有属性 'index'

django tables 'module' object has no attribute 'index'

我正在尝试使用 django tables 2 教程 (Link),但遇到了这个错误。

编辑:当我尝试访问 127.0.0.1:8000/tables/

时会发生这种情况

我卡在了教程的这一部分: "Hook the view up in your URLs, and load the page, you should see:" 它不显示此 table 而是显示下面显示的错误。

我尝试了其他问题中列出的解决方案,但没有帮助。有人可以帮我吗?

代码如下:https://github.com/karbfg10k/temp-work/tree/master/IMedMonitor/IMed

这是错误

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'

按照 Joel 的说明取消对 index 方法的注释,然后像在 people 方法中所做的那样呈现一个 html 页面,同时传递 table 数据。

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

页面下方大约 3/4(略微修改以适合您的示例):

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

在相应的 html 页面中添加适当的标签以呈现 table

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

我将您的模型文件更改为:

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)

更改模型文件后,确保 运行

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

您的 tables/urls.py 文件到:

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

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

您的 views.py 文件发送至:

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)

IMed/IMed/settings.py 中安装的应用到:

# 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',
]

现在,如果您 运行 服务器并转到此处:

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

people 视图将起作用,您可以在索引中复制与在 people 中相同的过程。