django request.POST vs request.method

by Buddy Lindsey on January 6, 2014

Properly processing requests in your views is important, and one of the most important things is knowing what HTTP verb is being used.

There are few ways to determine whether you are getting a GET or POST. The two most common ways people determine this is:

if request.POST:
    pass

or

if request.method == 'POST':
    pass

It is important to know the difference between the two. While they do get you to the same place there are a couple of caveats to note.

request.method

This actually returns a string of the method used with the request, and nothing else. This is important because you can use HTTP verbs without sending data.

request.POST

If you do a boolean check of request.POST it checks to make sure that there is data in the POST QueryDict dictionary. If there is data then it was a POST; if no data then it evaluates as false as if no POST happened.

The problem is you can do a post even without data and if you do you would get the following result: (captured from the shell)

request.POST
<QueryDict: {}>

Which means there is no data so your code says “this is not a POST” when it really is one.

Conclusion

If you don’t know how these two different parts of the framework work then it can lead to a lot of headache when you get results you don’t expect. I recommend to always use request.method instead of evaluating if the QueryDict has data in it from a GET or a POST. It can lead to headaches in logic in some few instances, and is more explicit as to what is being evaluated.

Related Posts:

{ 0 comments }

Quick and Dirty Write Your Own Bash Autocomplete

by Buddy Lindsey on September 16, 2013

bashTyping a command on the command line and hitting tab to get the rest of what you need is very nice. It is almost magical. Fortunately it is quite easy to write your own script to handle this for any application you have, or write.

This is a quick and dirty intro into how to do this.

All the Code You Need.

To get started there is some code that you just need to add with out really understanding so you can get something done.

_djangoadmin()
{
  local cur=${COMP_WORDS[COMP_CWORD]}
  COMPREPLY=( $(compgen -W "runserver collectstatic" -- $cur) )
}
complete -F _djangoadmin django-admin.py
  • First you are creating a new function called _djangoadmin to be called later.
  • Line 3 is boilerplate to just add to your function.
  • Line 4 is where the magic is. After the -W in the quotes is where you put your autocompleted commands.
  • Line 6 is saying that when you type in django-admin.py and hit tab run the _djangoadmin fuction.

Conclusion

After seeing this I quickly realized that since it is fairly simple to get started I need to offer an autocomplete script if I ever write a CLI utility for other people to use. It is also useful for writing your own autocomplete for utilities that don’ have it.

Related Posts:

{ 0 comments }

5 Project Management Lessons I Re-Learned

May 28, 2013

For the last few weeks I have spent a lot of my free time rebuilding the GoDjango site. GoDjango has been a lot of fun, and I have felt bad putting it on a hiatus to get a new job and get married. So I decided i’m going to relaunch the site and really do […]

Read the full article →

Adding Git Data to Your Bash Prompt

May 14, 2013

A well setup bash prompt can save you a lot of time, and be amazingly useful giving you all the information you need, quickly. Unfortunately, you need to set it up properly which is a bit tricky. We will walk you through setting up your prompt with git data. Bash 4+ First thing you need […]

Read the full article →

Quick Intro to Python Requests Library

May 7, 2013

Calling 3rd party services is an essential part of web development these days. I did a quick little python article on Basic urllib GET and POST With and Without Data. It was a good look into how python natively handles doing GET and POST HTTP actions. However, there is a better way, and that is […]

Read the full article →

Upgrade Bash to 4+ on OS X

April 30, 2013

Unfortunately, Apple has decided to ship an old version of bash shell. When I go back and forth between linux and OSX sometimes I hit minor inconsistencies because of this. One big one is the git-prompt scripts. As such I finally decided to upgrade to version 4 of bash. It is a very easy process, […]

Read the full article →

Adding Git Autocomplete to Bash on OS X

April 23, 2013

If you use git on the command line a lot then git bash autocomplete is an amazing tool to have in your toolbelt. Unfortunately, just by installing git you don’t get to use it. You need to add command line functionality to your environment. The easiest way to add autocomplete is by downloading a file […]

Read the full article →

Jenkins and Github Pull Requests

April 16, 2013

One of the things people love about travis-ci is it will build pull requests, but most people don’t realize Jenkins can do pull requests as well. It is also very simple to configure Jenkins to do pull requests using the correct plugin. Install the Plugin Install this plugin through the manage Jenkins admin section. Github […]

Read the full article →

Python Date and Datetime Objects – Getting to Know Them

April 9, 2013

Properly dealing with dates can be hard, but they don’t have to be as long as you understand the basics. I have had to deal a lot with dates lately with a few Django applications, and life got a whole lot easier once I figured out the basics with python. In this post we are […]

Read the full article →

Minimum Grep You Need to Know

April 2, 2013

You need to find a phrase in over 200 files worth of code. Manual searching is not a feasible option. If you are like me you know about grep, but it has always made you nervous. It is so powerful reading the man page was like a tech manual for an engine. Fortunately, getting the […]

Read the full article →