django_tables2を動作させようとしていますが、エラーが発生しています。私はすでにそれをインストールして、それはすでに私の設定になっています。Django Tables2を動作させるには
models.py
class Schedules(models.Model):
course_name = models.CharField(max_length=128, choices=COURSE_NAME_CHOICES, default='a-plus')
location = models.CharField(max_length=128, choices=LOCATION_CHOICES, default='south_plainfield')
room = models.CharField(max_length=128, choices=ROOM_CHOICES, default='A')
start_date = models.DateField(auto_now=False, auto_now_add=False, default=datetime.date.today)
start_time = models.CharField(max_length=128, choices=START_TIME_CHOICES, default='eight-thirty am')
end_time = models.CharField(max_length=128, choices=END_TIME_CHOICES, default='eight-thirty am')
instructor = models.CharField(max_length=128, choices=INSTRUCTOR_CHOICES, default='adewale')
total_hours = models.CharField(max_length=128, choices=TOTAL_HOURS_CHOICES, default='six')
hours_per_class = models.CharField(max_length=128, choices=HOURS_PER_CLASS_CHOICES, default='four_and_half')
frequency = models.CharField(max_length=128)
status = models.CharField(max_length=128, choices=STATUS_CHOICES)
interval = models.CharField(max_length=128, choices=INTERVAL_CHOICES, default='1 day')
initiated_by = models.CharField(max_length=128, null=True)
schedule_id = models.IntegerField(default=0)
def save(self, flag=True, *args, **kwargs):
super(Schedules, self).save()
if flag:
self.schedule_id = self.id + 10000
self.save(flag=False, *args, **kwargs)
tables.py
import django_tables2 as tables
from schedule.models import Schedules
class ScheduleTable(tables.Table):
class Meta:
model = Schedules
views.py
from schedule.models import Schedules
from schedule.tables import ScheduleTable
def search_Schedule(request):
context_dict = {}
if request.method == 'POST':
query = request.POST['course_name_search']
results = Schedules.objects.filter(course_name=query)
table = ScheduleTable(results)
if query:
context_dict['table'] = table
else:
context_dict['no_results'] = query
return render(request, "schedule/search_schedule.html", context_dict)
search_schedule.html
{% extends 'base.html' %}
{% load django_tables2 %}
{% block main_content %}
<form method="post" action="">
{% csrf_token %}
<label for="course_name_search">Course Name:</label>
<input type="text" name="course_name_search" id="course_name_search">
<input type="submit" name="submit">
</form>
<div id="result_panel">
{% if no_results %}
No results returned for <q>{{ no_results }}</q>
{% else %}
{% render_table table %}
{% endif %}
</div>
{% endblock %}
私は 'ValueError at/schedule/search_schedule /:期待されるテーブルまたはクエリセットではなく、strです。私は手紙の手順を踏んだと確信しています。私は間違って何をしていますか?
編集:ここではトレースバックです:GET
要求については
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/schedule/search_schedule/
Django Version: 1.10
Python Version: 3.6.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration',
'django_countries',
'localflavor',
'django_tables2',
'avtechpassport',
'students',
'billing',
'timesheet',
'wai_wdp',
'jobs',
'schedule']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template /home/jerry22/PycharmProjects/avtechpassport_proj1/templates/schedule/search_schedule.html, error at line 38
Expected table or queryset, not str 28 : <form method="post" action="">
29 : {% csrf_token %}
30 : <label for="course_name_search">Course Name:</label>
31 : <input type="text" name="course_name_search" id="course_name_search">
32 : <input type="submit" name="submit">
33 : </form>
34 : <div id="result_panel">
35 : {% if no_results %}
36 : No results returned for <q>{{ no_results }}</q>
37 : {% else %}
38 : {% render_table table %}
39 : {% endif %}
40 : </div>
41 : {% endblock %}
Traceback:
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jerry22/PycharmProjects/avtechpassport_proj1/schedule/views.py" in search_Schedule
81. return render(request, "schedule/search_schedule.html", context_dict)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
68. return template.render(context, request)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/backends/django.py" in render
66. return self.template.render(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/base.py" in render
208. return self._render(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/base.py" in _render
199. return self.nodelist.render(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/loader_tags.py" in render
174. return compiled_parent._render(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/base.py" in _render
199. return self.nodelist.render(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/loader_tags.py" in render
70. result = block.nodelist.render(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/defaulttags.py" in render
323. return nodelist.render(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/jerry22/.virtualenvs/jerry_avtechpassport_project/lib/python3.6/site-packages/django_tables2/templatetags/django_tables2.py" in render
132. raise ValueError('Expected table or queryset, not {}'.format(klass))
Exception Type: ValueError at /schedule/search_schedule/
Exception Value: Expected table or queryset, not str
完全トレースを表示してください – Alasdair