首页 > 解决方案 > 将 request.session 与 form.cleaned_data 一起使用时出现 DJANGO KeyError

问题描述

我有 2 个视图功能。一个获取表单数据并(最好)将其存储在 request.session 中。第二个调用此数据并使用它在搜索查询中进行过滤。我还有 2 个与这些视图相对应的 html 模板。这里是意见

def searchpatients(request):
form = PatientSearchForm(request.POST or None)
if form.is_valid():
    request.session['fdata'] = form.cleaned_data['first_name']
    request.session['ldata'] = form.cleaned_data['last_name']
context = {
    'form_search' : form,
    }
return render(request, 'polls/search.html', context)

def displayed_try(request):
    fdata = request.session['fdata']
    ldata = request.session['ldata']
    results = Patients.objects.filter(
    first_name__icontains = 'fdata'
    ).filter(
    last_name__icontains = 'ldata'
    ).values_list('first_name', 'last_name', 'phone_number', 'status')
    context = {
    'results' : results
    }
    return render(request, 'polls/displayed.html', context)

运行搜索功能时,我收到这样的关键错误

KeyError at /displayed/

'fdata' 请求方法:POST 请求 URL: http://localhost:8000/displayed/ Django 版本:2.0.6 异常类型:KeyError 异常值:
'fdata' 异常位置:C:\Users\msengar\AppData\Local\ Programs\Python\Python36-32\lib\site-packages\django\contrib\sessions\backends\base.py 在getitem中,第 55 行 Python 可执行文件:C:\Users\msengar\AppData\Local\Programs\Python\Python36- 32\python.exe Python 版本:3.6.5

谁能指出我正确的方向?我不确定如何从 Django 中的表单(在不同的 HTML 模板上)获取数据,并使用该表单数据在另一个 HTML 页面上搜索和显示。作为参考,我的 2 个 html 页面是 search.html(包含搜索表单)和 display.html(在列表中显示过滤后的数据)。搜索页面按预期工作,但 display.html 页面要么总是给我一些 keyerror(使用会话存储时),要么显示一个没有数据的空表(仅使用表单操作时)。最后,这里是 display.html 代码。

{% block content %}
<div class = "shit">
<table class="table table-hover indexed table-dark">
  <thead>
    <tr>
      <th scope = "col"> "First Name" </th>
      <th scope = "col"> "Last Name" </th>
      <th scope = "col"> "Phone" </th>
      <th scope = "col"> "Status" </th>
    </tr>
    </thead>
  <tbody id = "rtable">
    {% for x in results %}
    <tr>
      {% for v in x %}
        <td> {{ v }} </td>
      {% endfor %}
    </tr>
    {% endfor %}
  </tbody>
</table>
</div>
{% endblock %}

编辑——这是 urls.py——

urlpatterns = [
path('admin/', admin.site.urls),
path('', home_try, name = 'homepage'),
path('signup/', signup_try.as_view(), name = 'signup'),
path('add/', addpatients, name = 'tryadding'),
path('profile/', profile_try, name = 'tryprofile'),
path('accounts/', include('django.contrib.auth.urls')),
path('information/', information_try, name = 'tryinformation'),
path('logout/', logout_try, name = 'tryloggingout'),
path('login/', auth_views.login, name = 'login'),
path('login_error/', login_error_handle, name = 'tryloginerror'),
path('change_password/', auth_views.password_reset, name = 'passwordchangetry'),
path('table_view/', table_try, name = 'try_table'),
path('search/', searchpatients, name = 'try_search'),
path('displayed/', displayed_try, name = 'try_displayed'),

这是 search.html 代码——

<form method = "POST" action = "../displayed/" name = "pls">
<fieldset>
  <legend> Search Entry </legend>
  {% csrf_token %}
  {{ form_search.as_p }}
  <button type = "submit"> Search </button>
</fieldset>

谢谢大家,很抱歉这是多么混乱。请帮忙!

标签: pythonhtmldjango

解决方案


您将会话值存储到 fdata 而不是带引号first_name__icontains = fdata的 ' 'fdata

另一个技巧——使用Q连接查询。


推荐阅读