我试图更好地理解以下代码。它正在工作,但我只是不完全理解几个元素(下面的问题):
from django.shortcuts import render_to_response
from mywiki.wiki.models import Page
from django.http import HttpResponseRedirect
from django import forms
import markdown
class SearchForm(forms.Form):
text = forms.CharField(label='Enter search term')
def search_page(request):
if request.method =='POST':
f = SearchForm(request.POST)
if not f.is_valid():
return render_to_response('search.html', {'form':f})
else:
pages = Page.objects.filter(name__contains = f.cleaned_data['text'])
return render_to_response('search.html', {'form':f, 'pages':pages})
f = SearchForm()
return render_to_response('search.html', {'form':f})
specialPages = {'SearchPage':search_page}
def view_page(request, page_name):
if page_name in specialPages:
return specialPages[page_name](request)
try:
page = Page.objects.get(pk=page_name)
except Page.DoesNotExist:
return render_to_response('create.html', {'page_name':page_name})
content = page.content
return render_to_response('view.html', {'page_name':page_name, 'content':markdown.markdown(content)})
为什么我们需要指定
request.method
必须是POST
- 单击 HTML 按钮不会隐含地表示更改/影响某些内容的意图?还是POST
仅在操作影响数据库时才合适?这条线是如何
pages = Page.objects.filter(name__contains = f.cleaned_data['text'])
工作的?它需要模型Page(models.Model)
,但在这种情况下,“对象”和“过滤器”方法会做什么?中,
view_page
为什么我们需要添加(request)
return specialPages[page_name](request)
谢谢!