我对Django很新,需要帮助完成一项简单的任务。我有两个模板。
search.html
:呈现一个包含文本字段,提交按钮和POST /search_results/
search_results.html
:需要将数据库查询的结果呈现为/search_results/
在我的forms.py
文件中:
from django import forms
class SystemsForm(forms.Form):
serial_num = form.CharField(label="Serial no.", max_length=45)
在我的urls.py中
urlpatterns = patterns('',
url(r'^search/$', SystemSearch.as_view()),
url(r'^search_results/$', SystemResult.as_view()),
)
在我的views.py文件中
from django.views.generic.edit import FormView
from inventory.forms import SystemsForm
class SystemSearch(FormView):
# For displaying the form
template_name = 'inventory/search.html'
form_class = SystemsForm
class SystemResult(FormView):
template_name = 'inventory/search_result.html'
form_class = SystemsForm
# How to do these two things?
# Query the database model called Systems with the POST data
# -- Systems.objects.get(serial_num=??)
# pass context to search_result.html
我甚至不确定我是朝着正确的方向前进,还是完全偏离了我应该做的事情。