Reputation: 2355
This is my views.py file code(which is present in my app 'search' in django):
#!/usr/bin/python
from dango.http import HttpResponse
from skey import find_root_tags, count, sorting_list
from search.models Keywords
def front_page(request):
if request.method == 'POST' :
str1 = request.POST['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_tamplate('search/search.html')
response = template.render()
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))
1)I wanted to know since I'm importing 'skey' file in views.py, so do I need to keep this 'skey.py', 'xml.txt' and 10 xml documents as well in search app directory?
2) Since I'm redirecting after post to the 'results' views, so, How do I mention about it in the urls.py i.e. in "urlpatterns" .
3) Then do I need to mention the "context" in the "def front_page(request):" ?will this definition of views work fine without it because according to me , it is not necessary that we have to use context in every def of views.
Django is new to me and I don't have any hands on experience of it.So,
Please help.
Upvotes: 0
Views: 296
Reputation: 1033
keep 'skey' in the same app directory. keep xml documents under app directory is not good but acceptable idea, better choice is put them under some place like "/var/www/your-site/xml-data"
/ "d:/your-site/xml-docs/"
and access them via absolute path
see following code
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^search/$','search.views.front_page'),
url(r'^results/$','search.views.results'),
url(r'^admin/', include(admin.site.urls)),
)
the normal use is like this: (the get_template and render will automatically called)
from django.shortcuts import render_to_response as rr
def home(req):
# do some work here
return rr('your template file',
{ ... }, # your parameters
context_instance = RequestContext(req))
Upvotes: 1