挣扎着想出这个。我已经看到了这个错误代码出现的几个问题,但似乎都与数据没有被传递到模板渲染的上下文有关。我知道我的数据正在进入模板。脆弱的形式必须在幕后做一些让我失望的事情?奇怪的是,它在20分钟前完成了。我不知道我能改变什么会影响到这一点。
以下是我的观点:
class ContactListView(View):
template = 'support_tracker/contact_list.html'
def get(self,request,*args,**kwargs):
staff_id = kwargs.pop('staff_id',False)
if staff_id:
staff = Staff.objects.get(pk=staff_id)
else:
staff = request.user.staff
contact_list = staff.contacts.all()
search_term = request.GET.get('search_term',False)
sort_by = request.GET.get('sort_by',False)
filter_by = request.GET.get('filter_by',False)
##applying search term to determine contact_list
...
##
##sorting of contact_list
....
##
##filtering of contact_list
....
##
search_form = SearchForm()
sort_form = ContactListSortForm()
filter_form = FilterByStageForm()
##pagination to determine contacts
...
##
context = {
'staff':staff,
'contacts':contacts,
'search_form':search_form,
'sort_form':sort_form,
'filter_form':filter_form,
'filter_by':filter_by,
'search_term':search_term,
'sort_by':sort_by,
}
print context
return render(request,self.template,context)
我的表格:
##forms.py
class SearchForm(forms.Form):
search_term = forms.CharField(max_length=30,label='')
def __init__(self,*args,**kwargs):
super(SearchForm,self).__init__(*args,**kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-inline'
self.helper.form_method = 'GET'
self.helper.field_template = 'bootstrap3/layout/inline_field.html'
self.helper.layout = Layout(
InlineField('search_term',placeholder='Search'),
Submit('','Go',css_class='btn btn-primary'),
)
class FilterByStageForm(forms.Form):
filter_by = forms.ChoiceField(choices=get_stage_option_list(),required=False)
def __init__(self,*args,**kwargs):
super(FilterByStageForm,self).__init__(*args,**kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-inline'
self.helper.form_method = 'GET'
self.helper.field_template = 'bootstrap3/layout/inline_field.html'
self.helper.layout = Layout(
InlineField('filter_by',placeholder='Filter By'),
Submit('','Filter',css_class='btn btn-primary'),
)
class ContactListSortForm(forms.Form):
sort_by = forms.ChoiceField(choices=(##options),required=False)
def __init__(self,*args,**kwargs):
super(ContactListSortForm,self).__init__(*args,**kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-inline'
self.helper.form_method = 'GET'
self.helper.field_template = 'bootstrap3/layout/inline_field.html'
self.helper.layout = Layout(
InlineField('sort_by',placeholder='Sort By'),
Submit('','Sort',css_class='btn btn-primary'),
)
我的模板:
{% extends 'co_tools/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class='container'>
<div class='col-md-3 well'>
<h3><u>Search:</u></h3>
## This is the part that trips it up. If I take the forms out, the rest of the page loads fine
{% crispy search_form %}
{% crispy sort_form %}
{% crispy filter_form %}
<a href='/support/new_contact/' class='btn btn-primary'>New Contact</a>
</div>
...body...
</div>
{% endblock content %}
完整追溯
Request Method: GET
Request URL: http://127.0.0.1:8000/support/contact_list/
Django Version: 1.10.2
Python Version: 2.7.11
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'support_tracker',
'donor_reports',
'staff',
'financial_reports']
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 C:\...\project\support_tracker\templates\support_tracker\contact_list.html, error at line 8
cannot convert dictionary update sequence element #0 to a sequence 1 : {% extends 'co_tools/base.html' %}
2 : {% load crispy_forms_tags %}
3 :
4 : {% block content %}
5 : <div class='container'>
6 : <div class='col-md-3 well'>
7 : <h3><u>Search:</u></h3>
8 : {% crispy form %}
9 : <a href='/support/new_contact/' class='btn btn-primary'>New Contact</a>
10 : </div>
11 : <div class='col-md-9'>
12 : <table class='table table-condensed table-hover'>
13 : <thead>
14 : <th>Contact Name</th>
15 : <th>Phone Number</th>
16 : <th>Email</th>
17 : <th>Street Address</th>
18 : <th>City</th>
Traceback:
File "C:\...\project\env\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\...\project\env\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\...\project\env\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\...\project\env\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "C:\...\project\env\lib\site-packages\django\views\generic\base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "C:\...\project\support_tracker\views.py" in get
185. return render(request,self.template,context)
File "C:\...\project\env\lib\site-packages\django\shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "C:\...\project\env\lib\site-packages\django\template\loader.py" in render_to_string
68. return template.render(context, request)
File "C:\...\project\env\lib\site-packages\django\template\backends\django.py" in render
66. return self.template.render(context)
File "C:\...\project\env\lib\site-packages\django\template\base.py" in render
208. return self._render(context)
File "C:\...\project\env\lib\site-packages\django\template\base.py" in _render
199. return self.nodelist.render(context)
File "C:\...\project\env\lib\site-packages\django\template\base.py" in render
994. bit = node.render_annotated(context)
File "C:\...\project\env\lib\site-packages\django\template\base.py" in render_annotated
961. return self.render(context)
File "C:\...\project\env\lib\site-packages\django\template\loader_tags.py" in render
174. return compiled_parent._render(context)
File "C:\...\project\env\lib\site-packages\django\template\base.py" in _render
199. return self.nodelist.render(context)
File "C:\...\project\env\lib\site-packages\django\template\base.py" in render
994. bit = node.render_annotated(context)
File "C:\...\project\env\lib\site-packages\django\template\base.py" in render_annotated
961. return self.render(context)
File "C:\...\project\env\lib\site-packages\django\template\loader_tags.py" in render
70. result = block.nodelist.render(context)
File "C:\...\project\env\lib\site-packages\django\template\base.py" in render
994. bit = node.render_annotated(context)
File "C:\...\project\env\lib\site-packages\django\template\base.py" in render_annotated
961. return self.render(context)
File "C:\...\project\env\lib\site-packages\crispy_forms\templatetags\crispy_forms_tags.py" in render
215. c = self.get_render(context)
File "C:\...\project\env\lib\site-packages\crispy_forms\templatetags\crispy_forms_tags.py" in get_render
126. response_dict = self.get_response_dict(helper, context, is_formset)
File "C:\...\project\env\lib\site-packages\crispy_forms\templatetags\crispy_forms_tags.py" in get_response_dict
160. attrs = helper.get_attributes(template_pack=self.template_pack)
File "C:\...\project\env\lib\site-packages\crispy_forms\helper.py" in get_attributes
364. if self.form_action:
File "C:\...\project\env\lib\site-packages\crispy_forms\helper.py" in form_action
237. return reverse(self._form_action)
File "C:\...\project\env\lib\site-packages\django\urls\base.py" in reverse
91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "C:\...\project\env\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
344. self._populate()
File "C:\...\project\env\lib\site-packages\django\urls\resolvers.py" in _populate
217. dict(defaults, **pattern.default_kwargs),
Exception Type: TypeError at /support/contact_list/
Exception Value: cannot convert dictionary update sequence element #0 to a sequence
我不知道造成这种情况的原因。任何帮助将不胜感激。
答案 0 :(得分:0)
好的解决了这个问题。我不知道为什么这段特殊的代码导致了这个问题。
在我的urls.py中,我添加了以下四个url表达式...
来自django.conf.urls导入网址,包含 导入视图
urlpatterns = [
...
#meeting urls
url(r'^record_meeting/(?P<meeting_id>[0-9]+)/',views.EditMeetingView.as_view(),{'record':True}),
url(r'^record_meeting/$',views.EditMeetingView.as_view(),{'record':True}),
url(r'^edit_meeting/(?P<meeting_id>[0-9]+)/',views.EditMeetingView.as_view(),{'record':False}),
url(r'^add_meeting/$',views.EditMeetingView.as_view()),
]
第四个原本是url(r'^add_meeting/$',views.EditMeetingView.as_view(),{'record':False})
,显然是基于反复试验导致了这个问题。删除其他参数后,问题就消失了。有谁知道为什么这首先是一个问题?我还是不明白。