在cookies中的实现错误

34 浏览
0 Comments

在cookies中的实现错误

更新:我已经在我的帖子中添加了图片,请现在查看我想要的内容:

我的主页:

enter image description here

用户输入了单词'name'。

enter image description here

用户按下搜索按钮后,会得到列表和图表,但可以看到单词'name'在搜索栏中保留得更多,但我希望它仍然在那里。

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

我的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文档列表及其相应的计数和图表。

在服务器上运行该应用程序时,当用户在搜索栏中输入单词时,结果会在同一页上完美地显示,但是当用户按下搜索按钮时,搜索栏中的单词不会被保留。为了实现这一点,我使用了cookies,但是它报错了

'SafeUnicode' object has no attribute 'set_cookie'

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

0
0 Comments

问题:在使用cookies时出现的实现错误的原因以及解决方法。

在使用cookies时,我们通常需要设置cookie和获取cookie的值。设置cookie的方法是使用HttpResponse对象的set_cookie方法,如下所示:

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

获取cookie的值可以通过request.COOKIES['word']或者更安全的方法request.COOKIES.get('word', None)来实现。

此外,还可以通过将值传递给模板的方式,在模板中设置cookie的默认值。具体做法如下:

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

在模板中,需要更新搜索栏的字段,代码如下:


总之,cookies和sessions是一种在多个请求之间存储值的方式。如果需要使用cookies来保持特定值,可以按照上述方法进行设置和获取。如果遇到问题,可以在讨论区继续讨论。

0
0 Comments

问题的出现原因:使用错误的方法设置cookie,导致无法正确保存数据。

解决方法:使用正确的方法将数据保存在会话中。可以使用request.session['word'] = request.POST['word']来保存数据。此方法使用django处理其他事务,并确保数据正确保存。

在设置cookie时,如果使用错误的方法,可能会导致无法正确保存数据。而正确的方法是将数据保存在会话中。可以使用request.session['word'] = request.POST['word']来保存数据。这样,django会处理其他事务,并确保数据正确保存。

如果按照错误的方法进行操作,可能会遇到问题。例如,当尝试保存数据时,发现数据并没有被正确保留。这可能会导致代码无法正常工作。

因此,要解决这个问题,我们应该使用正确的方法将数据保存在会话中。这可以通过将数据赋值给request.session['word']来实现。这样,数据将被正确保存,并且可以在后续的代码中使用。

更多关于如何使用会话的信息可以参考会话的使用指南

0