我正在将ajax实现为django表单。为此,我正在遵循this教程。但是,当我单击“新书”按钮或“更新”按钮时,出现一个奇怪的错误。
“预期的str实例,找到了BoundField”
代替打开模态,只显示一小行。
完整追溯
Request Method: GET
Request URL: http://127.0.0.1:8000/users/book/create/
Django Version: 2.1.2
Python Version: 3.7.2
Installed Applications:
['users.apps.UsersConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrap_modal_forms',
'main',
'first_app',
'campaign',
'invoice',
'taggit',
'taggit_selectize',
'rest_framework',
'django_filters',
'django_extensions',
'shop',
'search',
'cart',
'orders',
'widget_tweaks']
Installed Middleware:
('whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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 C:\Users\HP\Desktop\Crowdsocial\templates\partial_book_form.html, error at line 3
sequence item 7: expected str instance, BoundField found
1 : {% load widget_tweaks %}
2 :
3 : {% for field in form %}
4 : <div class="form-group{% if field.errors %} has-error{% endif %}">
5 : <label for="{{ field.id_for_label }}">{{ field.label }}</label>
6 : {% render_field field class="form-control" %}
7 : {% for error in field.errors %}
8 : <p class="help-block">{{ error }}</p>
9 : {% endfor %}
10 : </div>
11 : {% endfor %}
12 :
Traceback:
File "C:\python3\Lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\python3\Lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:\python3\Lib\site-packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\HP\Desktop\Crowdsocial\users\views.py" in create_book
194. return save_book_form(request, form, 'partial_book_create.html')
File "C:\Users\HP\Desktop\Crowdsocial\users\views.py" in save_book_form
178. data['html_form'] = render_to_string(template_name, context, request=request)
File "C:\python3\Lib\site-packages\django\template\loader.py" in render_to_string
62. return template.render(context, request)
File "C:\python3\Lib\site-packages\django\template\backends\django.py" in render
61. return self.template.render(context)
File "C:\python3\Lib\site-packages\django\template\base.py" in render
171. return self._render(context)
File "C:\python3\Lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "C:\python3\Lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\python3\Lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\python3\Lib\site-packages\django\template\loader_tags.py" in render
188. return template.render(context)
File "C:\python3\Lib\site-packages\django\template\base.py" in render
173. return self._render(context)
File "C:\python3\Lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "C:\python3\Lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File
render_annotated中的“ C:\ python3 \ Lib \ site-packages \ django \ template \ base.py” 904. return self.render(context)
渲染中的文件“ C:\ python3 \ Lib \ site-packages \ django \ template \ defaulttags.py” 216. return mark_safe(''。join(nodelist))
异常类型:/ users / book / create /中的TypeError 异常值:序列项目7:预期的str实例,找到了BoundField
urls.py
urlpatterns = [
path('book_list/', views.book_list, name='book_list'),
path('book/create/', views.create_book, name='book_create'),
path('<int:pk>/book/update/', views.book_update, name='book_update'),
path('<int:pk>/book/delete/', views.book_delete, name='book_delete'),
]
form.py
class EducationForm(forms.ModelForm):
class Meta:
model = Education
fields = ['degree','school','edu_start_date','edu_end_date']
观看次数
def save_book_form(request, form, template_name):
data = dict()
if request.method == 'POST':
if form.is_valid():
form.save()
data['form_is_valid'] = True
books = Education.objects.all()
data['html_book_list'] = render_to_string('profile_settings.html', request, {
'books': books
})
else:
data['form_is_valid'] = False
context = {'form': form}
data['html_form'] = render_to_string(template_name, context, request=request)
return JsonResponse(data)
def create_book(request):
if request.method == 'POST':
form = EducationForm(request.POST)
else:
form = EducationForm()
return save_book_form(request, form, 'partial_book_create.html')
def book_list(request):
books = Education.objects.all()
return render(request, 'book_list.html', {'books': books})
模板
partial_book_form.html
{% load widget_tweaks %}
{% for field in form %}
<div class="form-group{% if field.errors %} has-error{% endif %}">
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{% render_field field class="form-control" %}
{% for error in field.errors %}
<p class="help-block">{{ error }}</p>
{% endfor %}
</div>
{% endfor %}
book_list.html
{% extends 'base.html' %}
{% load static %}
{% block javascript %}
<script src="{% static 'books/js/books.js' %}"></script>
{% endblock %}
{% block content %}
<h1 class="page-header">Books</h1>
<p>
<button type="button" class="btn btn-primary js-create-book" data-url="{% url 'users:book_create' %}">
<span class="glyphicon glyphicon-plus"></span>
New book
</button>
</p>
<table class="table" id="book-table">
<thead>
<tr>
<th>#</th>
<th>Degree</th>
<th>School</th>
<th>Start Date</th>
<th>End date</th>
</tr>
</thead>
<tbody>
{% include 'partial_book_list.html' %}
</tbody>
</table>
<div class="modal fade" id="modal-book">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
{% endblock %}
partial_book_create.html
<form method="post" action="{% url 'users:book_create' %}" class="js-book-create-form">
{% csrf_token %}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Create a new book</h4>
</div>
<div class="modal-body">
{% include 'partial_book_form.html' %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Create book</button>
</div>
</form>
partial_update_form.html
<form method="post" action="{% url 'users:book_update' form.instance.pk %}" class="js-book-update-form">
{% csrf_token %}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Update book</h4>
</div>
<div class="modal-body">
{% include 'partial_book_form.html' %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Update book</button>
</div>
</form>