0

UPDATION: I have updated my post with images, please see now what I exactly want :

My front_page:

enter image description here

user hase entered the word 'name'.

enter image description here

after the user presses search, the user is getting the list and charts , but you can see that the word 'name' is more retained in the search bar but I want it to be there.

Now did you get my question ?

My views.py file code:

#!/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)

I have created an app using django search that searches the keywords in 10 xml documents and return the frequency of the occurrence of the keywords for each file which is displayed as hyperlinked list of xml documents with their respective counts and the charts.

On running the app on server, as user enters the word in the search bar , the results are displayed on the same page perfectly but the word is not retained in the search bar as user presses search tab . For that to be done, I have made use of cookies but it is giving error

'SafeUnicode' object has no attribute 'set_cookie'

why? I'm new to django, so please help

POOJA GUPTA
  • 2,295
  • 7
  • 32
  • 60
  • y u havn't used `render_to_response` ??? see the documentation [here](https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render-to-response) just pass the `request.POST['word'] ` in dictionary and access it in your template useing `{{ }}` – Priyank Patel Jul 11 '12 at 05:44

2 Answers2

2

I take it you want to use cookies, this should help you get started: https://docs.djangoproject.com/en/dev/topics/http/sessions/?from=olddocs/#using-cookie-based-sessions

then we have this Django Cookies, how can I set them?

fundamentally to set the cookie you would need to :

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

to get the cookie you would only need to request.COOKIES['word'] or a safer method would be 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))

on your template you should update the search bar field:

<input type="text" name="word" value="{{ word }}" />

please go through the entire docs when you get a chance, yes I know their quite extensive, but their well worth it...

Community
  • 1
  • 1
Samy Vilar
  • 10,800
  • 2
  • 39
  • 34
  • hey it's not working , please see my code carefully , when user enters word and presses search , he is directed to the front_page.html and i.e. the same page where user was shown the search bar , just below it , the results are shpwn – POOJA GUPTA Jul 11 '12 at 07:06
  • @POOJAGUPTA can you be more specific, do you get an error? please update your question with your current code and exceptions, if any. – Samy Vilar Jul 11 '12 at 07:55
  • this is completely different from your original problem, and has nothing to do with cookies, which are nothing more than a way to remember states on a per client basis, nothing to do with HTML per se, you need to grab the word, pass it to your template and set it as the default value in your text input. – Samy Vilar Jul 11 '12 at 08:43
  • I don't know , I have been advised to use cookies to retain the word by my mentor when he saw this app, but you are saying to pass the word into my template which can be done through context and does'nt context takes a single argument ? – POOJA GUPTA Jul 11 '12 at 08:50
  • @POOJAGUPTA cookies and sessions in general are ways to store values acros multiple requests, if you need to you can use them, either way I've update the answer to what you maybe trying to achieve. – Samy Vilar Jul 11 '12 at 08:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13726/discussion-between-pooja-gupta-and-samy-vilar) – POOJA GUPTA Jul 11 '12 at 10:00
1

Instead of response.set_cookie(...) , you can set it as:

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

django handles other things. For more information refer How to use sessions

Rohan
  • 52,392
  • 12
  • 90
  • 87