将request.session与form.cleaned_data

时间:2018-07-24 19:14:53

标签: python html django

我有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)

AND

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 要求网址:http://localhost:8000/displayed/ Django版本:2.0.6 异常类型:KeyError 异常值:
'fdata' 例外位置: getitem 中的C:\ Users \ msengar \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ contrib \ sessions \ backends \ base.py 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页面要么总是给我带来一些键盘错误(使用会话存储时),要么显示一个没有数据的空表(仅使用表单操作时)。最后,这是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>

谢谢大家,很抱歉这太混乱了。请帮助!

1 个答案:

答案 0 :(得分:0)

您正在将会话值存储到fdata 用引号first_name__icontains = fdata代替“ fdata

另一个技巧-使用https://www.youtube.com/watch?v=2VsYakH3OUE来简化查询。