【问题标题】:implementation errors in cookiescookie 中的实现错误
【发布时间】:2012-07-10 16:20:02
【问题描述】:

更新:我已经用图片更新了我的帖子,现在请看看我到底想要什么:

我的首页:

用户输入了“姓名”这个词。

在用户按下搜索后,用户正在获取列表和图表,但您可以看到“名称”这个词更多地保留在搜索栏中,但我希望它在那里。

现在你明白我的问题了吗?

我的views.py文件代码:

#!/usr/bin/python 

from django.core.context_processors import csrf
from django.template import loader, RequestContext, Context
from django.http import HttpResponse
from search.models import Keywords
from django.shortcuts import render_to_response as rr
import Cookie

def front_page(request):

    if request.method == 'POST' :
        from skey import find_root_tags, count, sorting_list
        str1 = request.POST['word'] 
        str1 = str1.encode('utf-8')
        list = []
        for i in range(count.__len__()):
            count[i] = 0
        path = '/home/pooja/Desktop/'
        fo = open("/home/pooja/Desktop/xml.txt","r")

        for i in range(count.__len__()):

            file = fo.readline()
            file = file.rstrip('\n')
            find_root_tags(path+file,str1,i)    
            list.append((file,count[i]))

        for name, count1 in list:
            s = Keywords(file_name=name,frequency_count=count1)
            s.save()
        fo.close()

        list1 = Keywords.objects.all().order_by('-frequency_count')
        t = loader.get_template('search/front_page.html')
        c = RequestContext(request, {'list1':list1,
        })
        c.update(csrf(request))
        response = t.render(c)
        response.set_cookie('word',request.POST['word'])
        return HttpResponse(response)

    else :  
        str1 = ''
        template = loader.get_template('search/front_page.html')
        c = RequestContext(request)
        response = template.render(c)
        return HttpResponse(response)

我创建了一个使用 django 搜索的应用程序,该应用程序在 10 个 xml 文档中搜索关键字,并返回每个文件的关键字出现频率,该文件显示为 xml 文档的超链接列表及其各自的计数和图表。

在服务器上运行应用程序时,当用户在搜索栏中输入单词时,结果会完美地显示在同一页面上,但当用户按下搜索选项卡时,该单词不会保留在搜索栏中。为此,我使用了 cookie,但它给出了错误

'SafeUnicode' object has no attribute 'set_cookie'

为什么?我是 django 新手,请帮忙

【问题讨论】:

  • 你没用过render_to_response ???查看文档here 只需在字典中传递request.POST['word'] 并使用{{ }} 在模板中访问它

标签: python django linux ubuntu-10.04


【解决方案1】:

我认为您想使用 cookie,这应该可以帮助您入门:https://docs.djangoproject.com/en/dev/topics/http/sessions/?from=olddocs/#using-cookie-based-sessions

那么我们有这个Django Cookies, how can I set them?

从根本上设置您需要的 cookie:

 resp = HttpResponse(response)
 resp.set_cookie('word', request.POST['word'])

要获取 cookie,您只需 request.COOKIES['word'] 或更安全的方法是 request.COOKIES.get('word', None)

from django.shortcuts import render_to_response
...

c = {}
c.update(csrf(request))
c.update({'list1':list1, 'word':request.POST['word']})
return render_to_response('search/front_page.html', 
               c,
               context_instance=RequestContext(request))

在您的模板上,您应该更新搜索栏字段:

<input type="text" name="word" value="{{ word }}" />

如果有机会,请阅读整个文档,是的,我知道它们相当广泛,但它们非常值得……

【讨论】:

  • 嘿,它不起作用,请仔细查看我的代码,当用户输入单词并按搜索时,他会被定向到front_page.html,即显示搜索栏的同一页面,就在下面它,结果是 shpwn
  • @POOJAGUPTA 你能说得更具体点吗,你有错误吗?请使用您当前的代码和异常(如果有)更新您的问题。
  • 这与您的原始问题完全不同,并且与 cookie 无关,这只不过是一种基于每个客户端记住状态的方式,与 HTML 本身无关,您需要抓取单词,将其传递给您的模板并将其设置为文本输入中的默认值。
  • 我不知道,我的导师在看到这个应用程序时曾建议我使用 cookie 来保留单词,但您说要将单词传递到我的模板中,这可以通过上下文和上下文不接受单个参数吗?
  • @POOJAGUPTA cookie 和会话通常是跨多个请求存储值的方法,如果您需要,您可以使用它们,无论哪种方式我已经更新了您可能试图实现的答案。
【解决方案2】:

代替response.set_cookie(...),您可以将其设置为:

request.session['word'] = request.POST['word']

django 处理其他事情。更多信息请参考How to use sessions

【讨论】:

  • 它不工作............单词应该保留在那里......但没有这样的事情发生
猜你喜欢
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多