2012-07-11 69 views
0

更新用:我已經更新了我的職務與圖片,請現在看看我到底要:實施錯誤

我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

回答

1

相反的response.set_cookie(...),你可以將其設置爲:

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

的Django處理其他事情。欲瞭解更多信息,請參閱How to use sessions

+0

它不工作............字應保留有.......但是沒有這樣的事情發生 – 2012-07-11 07:01:40

2

我想你想使用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