0

更新: 投稿を画像で更新しました。正確に何が欲しいかを今すぐ確認してください:

私のフロントページ:

ここに画像の説明を入力

ユーザー 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)

10個のxmlドキュメントでキーワードを検索し、各ファイルのキーワードの出現頻度を返すdjango検索を使用してアプリを作成しました。これは、xmlドキュメントのハイパーリンクリストとそれぞれのカウントとグラフとして表示されます。

サーバーでアプリを実行すると、ユーザーが検索バーに単語を入力すると、結果は同じページに完全に表示されますが、ユーザーが検索タブを押すと、単語は検索バーに保持されません。そのために、Cookieを使用しましたが、エラーが発生しています

'SafeUnicode' object has no attribute 'set_cookie'

なぜ?私はdjangoが初めてなので、助けてください

4

2 に答える 2

2

私はあなたがクッキーを使いたいと思っています。これはあなたが始めるのに役立つはずです:

次に、このDjango Cookie があります。どのように設定できますか?

基本的に、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 }}" />

機会があれば、ドキュメント全体を確認してください。はい、かなり広範囲に及ぶことは知っていますが、それだけの価値があります...

于 2012-07-11T06:02:31.790 に答える
1

の代わりにresponse.set_cookie(...)、次のように設定できます。

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

django は他の処理も行います。詳細については、セッションの使用方法を参照してください。

于 2012-07-11T05:37:30.887 に答える