0

I have searched around and see that most are pointing to a search that was created by Julien Phalip: http://julienphalip.com/post/2825034077/adding-search-to-a-django-site-in-a-snap

Also the answer seems to be here: Very simple user input in django

However I am very new to Django and wanted to create a view where I actually understand what is happening so I have been going through the official Django and the Tango with Rango tutorials but I do not see a straightforward example of what I am trying to understand in regards to a simple form search. The main question I have is why is POST used in the example instead of GET? I thought POST was used to "create" data entries in mysql whereas GET is used to lookup/search for data entries? Am I missing something fundamental about using one vs the other?

I have the following simple example from my app:

models.py

class hardware(models.Model):
    text = models.CharField(max_length=200, unique=TRUE)
    pub_date = models.DateTimeField(default=timezone.now)
    def __unicode__(self):
        return self.text
class Barcode(models.Model):
    hardware = models.ForeignKey(Hardware)
    text = models.CharField(max_length=50)
    pub_date = models.DateTimeField(default=timezone.now)
    def __unicode__(self):
        return self.text

forms.py

class HardwareForm(forms.modelForm):
    class Meta:
        model = Hardware
        fields = ['text'}

views.py

def hardware_search(request):
    if request.method == 'POST':
        search_id = request.POST.get('textfield', None)
        try:
            hardwarename = Hardware.objects.get(text = search_id)
            html = ("<H1>%s</H1>", hardwarename)
            return HttpResponse(html)
         except Hardware.DoesNotExist:
            return HttpResponse("no such hardware found")   
    else:
        return render(request, 'search.html')

search.html

<form method="POST" action="/hardware_search.html">
{% csrf_token %}
<input type="text" name="textfield">

<button type="submit">Upload text</button>
</form>

My questions are is this the most simple way to request user input to search for and generate the search results? Why is POST used? I plugged in this code and it does seem to work but i just can't understand why.

Secondly how can I display asssociated foreignkey class along with the main class 'hardware' search results? Does the ForeignKey association give a shortcut way of displaying that data as well?

thanks!

Community
  • 1
  • 1
Donnie W
  • 11
  • 3

2 Answers2

0

The W3 has an excellent introduction to POST vs GET here. There is a lot to be said for why someone might use POST or GET, and what their roles should be. You are probably more interested in the differences from the user's (browser's) perspective. The biggest differences between using POST and GET in a browser, is that the GET request will display the parameters in the URL. Change your form to GET to see for yourself. The user will be taken to:

/hardware_search.html?textfield=Upload%20text

As opposed to where they are taken to when the form action is POST:

/hardware_search.html

The value of the textfield field is still sent to the server, but is not visible in the URL.

There are quite a few other differences in the behavior of GET and POST in form submission. I would highly recommend reading over that introduction by the W3.

AlexMeng
  • 833
  • 8
  • 15
  • Thanks Alex that makes sense to me now. Since my current data is not sensitive I should be okay to expose that info but may have to switch eventually since I plan to do some validation later. – Donnie W Dec 09 '14 at 01:09
0

You're right, POST is not really appropriate for a search form. Using GET here would be better.

The other thing wrong is that there's no need at all for a ModelForm, or really for any kind of Django form. You're not doing any validation, you're not even using the form for output, so it would be better to leave that out altogether. That makes the view look like this:

def hardware_search(request):
    query = request.GET.get('textfield', None)
    if query:
        try:
            hardwarename = Hardware.objects.get(text = query)
            html = ("<H1>%s</H1>", hardwarename)
            return HttpResponse(html)
         except Hardware.DoesNotExist:
            return HttpResponse("no such hardware found")   
    else:
        return render(request, 'search.html')

and you can change the form action to GET.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks Daniel I am still sorting out how I want the output presented so it seems I have more than a few ways to do that so thanks for the info. – Donnie W Dec 09 '14 at 01:11