实施错误

问题描述:

更新用:我已经更新了我的职务与图片,请现在看看我到底要:实施错误

我front_page:

enter image description here

用户HASE输入的词“名”。

enter image description here

用户按下搜索后,用户越来越列表和图表,但你可以看到这个词“名字”更保留在搜索栏,但我想它在那里。

现在您是否收到我的问题?

我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

yu havn't used'render_to_response' ??? ???参见[这里]的文档(https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render-to-response)刚刚通过了'request.POST [“字”]'在字典和访问它在你的模板期运用'{{}}' – 2012-07-11 05:44:42

相反的response.set_cookie(...),你可以将其设置为:

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

的Django处理其他事情。欲了解更多信息,请参阅How to use sessions

+0

它不工作............字应保留有.......但是没有这样的事情发生 – 2012-07-11 07:01:40

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

然后我们有这个Django Cookies, how can I set them?

从根本上设置您需要的饼干:

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 }}" /> 

请到通过整个文档时,你得到一个机会,是的,我知道他们的相当广泛,但它们很值得..

+0

嘿它没有工作,请仔细看我的代码,当用户输入文字并按下搜索时,他被引导到front_page.html并且即,用户被显示在搜索栏的同一页面,正下方,结果是shpwn – 2012-07-11 07:06:04

+0

@POOJAGUPTA你可以更具体一点,你有错误吗?请使用您当前的代码和例外更新您的问题,如果有的话。 – 2012-07-11 07:55:08

+0

@ samy.vihar:请看我更新的帖子,你会得到知道我的问题...... – 2012-07-11 08:38:11