POOJA GUPTA
POOJA GUPTA

Reputation: 2355

implementation difficulties in some part of views.py file in django cms

My views.py code:

from django.template import Context, loader, RequestContext
from django.http import HttpResponse
from skey import find_root_tags, count, sorting_list
from search.models import Keywords

def front_page(request):

    if request.method == 'get' :
        str1 = request.getvalue['word'] 
        fo = open("xml.txt","r")
        for i in range(count.__len__()):

            file = fo.readline()
            file = file.rstrip('\n')
            find_root_tags(file,str1,i)    

            list.append((file,count[i]))

            sorting_list(list)

        for name, count in list:
            s = Keywords(file_name=name,frequency_count=count)
            s.save()

        fo.close()
        return HttpResponseRedirect('/results/')

    else :  
        str1 = ''
        list = []
        template = loader.get_template('search/front_page.html')
        c = RequestContext(request)
        response = template.render(c)
        return HttpResponse(response)

def results(request):

    list1 = Keywords.objects.all()
    t = loader.get_template('search/results.html')
    c = Context({'list1':list1,
    })

    return HttpResponse(t.render(c))

@this for everyone.

the flow is this:

1) I run my app on the server .

2)It shows me the search page due to the else part of the view "def front_page(request)", now I want to execute the if part of the view "def front_page(request)" because I want to execute my python code written there and the redirected to the view "def results(request)", how can I do that ?

3) what should I mention in "action" of the front_page.html and in urls.py so that I can get back to the same view again. because I could'nt get back to the same view that I want it is repetitively showing me the same search page.Please help.

Upvotes: 0

Views: 93

Answers (2)

Ctrlspc
Ctrlspc

Reputation: 1596

To enlarge upon the answer posted by @Barnaby....by using action='#' your form will be posted to the same url as the url used in the get request for the form. Then in your view code, you have logic that says - if the request for this url is a GET request then do the work to configure the form, otherwise, you assume it is a POST and then you can handle the response.

Additionally I would advise that the your view explicitly checks that the request is a POST and if not make the assumption that it is a GET, rather than the other way around (as you have it), this is safer, as GET and POST are not the only request types, and you definitely need to know that you are dealing with a POST request if you want to deal with variables submitted in the POST request.

Hope that helps

Upvotes: 1

Barnaby Shearer
Barnaby Shearer

Reputation: 459

Short answer: action="#". This is a HTML trick to post back to the current URL.

The general answer to how to reference a view in a template is to use the url tag. You may also want to consider using Django's forms functionality.

Upvotes: 0

Related Questions