0

I'm very new to Django and need help with a simple task. I have two templates.

  1. search.html: renders a simple form with a text field an a submit button and POSTs to /search_results/
  2. search_results.html: need to render the result of database query POSTed to /search_results/

In my forms.py file:

from django import forms

class SystemsForm(forms.Form):
    serial_num = form.CharField(label="Serial no.", max_length=45)

In my urls.py

urlpatterns = patterns('',
                       url(r'^search/$', SystemSearch.as_view()),
                       url(r'^search_results/$', SystemResult.as_view()),
                       )

In my views.py file

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

I'm not even sure if I'm heading in the correct direction or completely off from what I should be doing.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Sanket
  • 746
  • 3
  • 13
  • 26
  • 1
    If you're not confident with class based views yet, try implementing it with function based views. The [forms chapter](http://www.djangobook.com/en/2.0/chapter07.html) from the Django book might be helpful (a lot of the book is outdated, but I think that chapter would help you). Finally, you don't really need two templates here unless you really want to. You should be able to display the form and results with one view and template. Good luck! – Alasdair Mar 16 '15 at 21:36
  • I think you should use a ListView, something like http://stackoverflow.com/questions/6406553/django-class-based-view-listview-with-form The only problem with that answer is that is uses POST to do a search query, which is wrong. – ignacio.munizaga Mar 17 '15 at 00:25
  • @ignacia. What do you say using POST to do a search query is wrong? – Sanket Mar 17 '15 at 21:31

0 Answers0