这是我的views.py文件
#!/usr/bin/python
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
#from django.shortcuts import render_to_response as rr
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_template('search/search.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))
这是我的results.html(存储在/home/pooja/Desktop/my_templates/search/results.html中, (这里搜索是我的应用名称):
<html>
<body bgcolor = "#9ACD32">
<style type="text/css">
h1 {
position: absolute;
top: 200px;
left: 480px;
}
form #Edit1 { position: relative;
top: 245px;
left: 480px;
}
form #Edit2 { position: relative;
top: 220px;
left: 680px;
}
</style>
<font size="5" face="arial" color="#0000FF">
<h1>Search Page</h1>
</font>
<hr/>
<br/>
<br/>
<Form Method ="POST">
<div id="Edit1">
<INPUT TYPE = 'text' name ='word' VALUE ="">
</div>
<div id="Edit2">
<INPUT TYPE = "Submit" VALUE = "Search">
</div>
</FORM>
</body>
</html>
当我在服务器上运行此应用时,search.html页面即将推出,当我输入一些单词“book”时,它给了我这个错误:
Using the URLconf defined in mysite.urls, Django tried these URL patterns,in this order:
^search/$
^admin/
The current URL, front_page/, didn't match any of these.
为什么会这样?我将所有skey.py,xml文档和xml.txt存储在搜索应用程序中。
答案 0 :(得分:0)
表单上的操作是/front_page/
。如果您希望它转到当前网址,那么您应该完全省略action
属性。