更新用:我已經更新了我的職務與圖片,請現在看看我到底要:實施錯誤
我front_page:
用戶HASE輸入的詞「名」。
用戶按下搜索後,用戶越來越列表和圖表,但你可以看到這個詞「名字」更保留在搜索欄,但我想它在那裏。
現在您是否收到我的問題?
我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,所以請大家幫忙
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