使用FormView类中的POST数据查询数据库

时间:2015-03-16 20:03:30

标签: django django-forms

我对Django很新,需要帮助完成一项简单的任务。我有两个模板。

  1. search.html:呈现一个包含文本字段,提交按钮和POST /search_results/
  2. 的简单表单
  3. search_results.html:需要将数据库查询的结果呈现为/search_results/
  4. 在我的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
    

    我甚至不确定我是朝着正确的方向前进,还是完全偏离了我应该做的事情。

0 个答案:

没有答案
相关问题