【问题标题】:Django save history of get requestDjango保存获取请求的历史
【发布时间】:2015-02-07 23:01:52
【问题描述】:

我正在开发一个 django 应用程序,人们在该应用程序中输入他们的 ID 并获得他们的活跃客户。

这已经解决了,我想要的是每次有人尝试输入他们的 ID 时保存

这是视图:

def ingreso_cliente(request):
query_string = ''
found_entries = None
activo = None
#sin buscar
retorno = '3'
today=datetime.now

if ('idcliente' in request.GET) and request.GET['idcliente'].strip():
    #filtro por el usuario
    query_string = request.GET['idcliente']
    entry_query = get_query(query_string, ['cliente__ci'])
    #filtro por el mes
    found_entries = Pagos.objects.filter(entry_query).order_by('cliente__ci')

    activo = found_entries.filter(Q(mes_desde__lte=today)&Q(mes_hasta__gte=today))
    if (not activo):
        #deudor
        retorno = '2'
    else:
        #activo
        retorno = '1'



return render_to_response('ingreso_cliente.html', { 'retorno':retorno, 'today':today }, context_instance=RequestContext(request))

这是我用来输入客户 ID 的模板,我会得到他是否处于活动状态:

<form method="get" action="/home/ingreso_cliente/">
<input type="text" name="idcliente" id="idcliente"/>
<input type="submit" value="Buscar"/>
</form>

hora: {{today}}

{% if retorno == '1' %}
    <span>Cliente Activo</span>
{% elif retorno == '2'%}
    <span>Cliente Deudor</span>
{% endif %} 

它是一个获取请求,为了保存在数据库中,我需要一个 POST,但是通过 POST,我无法从数据库中获取数据以了解其是否处于活动状态。有什么办法吗?

【问题讨论】:

  • 为什么不能在 GET 请求中保存一些内容?您可以在视图结束时完美地创建/保存对象。
  • 附言。如果您通过 POST 提交表单,您也可以从数据库中获取用户 - 对视图没有影响
  • 我不知道我可以用 GET 保存,我该怎么做?
  • 我试图让用户使用 POST 但没有任何结果

标签: django forms post get


【解决方案1】:

通过 GET 或 POST 提交表单没有区别。

在视图的末尾(在return 之前不久),执行以下操作:

LoginAttempt.objects.create(username=query_string, active=retorno)

其中LoginAttempt 是一个保存登录尝试的模型。这并不理想,但您明白了,可以改用logging 接口或您想要记录尝试的任何内容。

PS。您的表单也可以与 POST 完美配合,但显然您需要在 request.POST 而不是 request.GET 中查找您的 idcliente

【讨论】:

    猜你喜欢
    • 2012-01-25
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 2015-02-20
    • 1970-01-01
    相关资源
    最近更新 更多