Saturday, July 31, 2010

Django csrf_token Template Tag Fix

I was pretty annoyed when I realized that the required Django template tag {% csrf_token %} printed the following code.
<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='6bda3605af31dd8595d2a67d0dda827b' /></div>
As the developer, I should have complete control over what HTML is being sent to the browser. After some tinkering around, I came up with this method of stripping it down to only the token.
{% with csrf_token as csrf_token_clean %}{{ csrf_token_clean }}{% endwith %}
Here is an example of how I used it.
<input type="hidden" name="csrfmiddlewaretoken" value="{% with csrf_token as csrf_token_clean %}{{ csrf_token_clean }}{% endwith %}" />

I was expecting to use the |cut filter to strip off the generated HTML, but I didn't need to. Enjoy!

Tuesday, June 8, 2010

Condition Opposites

When writing if statement conditions, you may want to return a true value if the condition is false. In PHP, you would simply throw an exclamation point before your condition to make it return the opposite value. Here is a simple example to help visualize how this might be used.
$var1 = false;
if( !( $var1 ) ){
echo 'var1 is false';
Since the value of var1 was not true, the if statement would execute. To accomplish this in Python, you would include the word "not" before your condition. Here is an example of how the previous example would look in Python.
var1 = False
if not var1:
print('var1 is false')
If you have any further questions, feel free to ask in the comments below.

Sunday, February 28, 2010

Boolean Operators

Much like the discovery in my post about multiple condition if statements, and their capitalization strictness, I stumbled upon another capitalization difference between PHP and Python. In PHP, you are able to type true however you like (true, TRUE, True, etc). In Python, however, you must use True and False. This may be helpful later on after you spend 10 minutes trying to figure out why "true" is not defined. Enjoy!

Reading Meta Data

Reading and using someones IP address is something I do quite frequently, so I was happy to learn that it is pretty easy to read a visitors meta data in Django. I will explain how to view a visitors IP address, and that should get you started. For information on how to read other information, and a list of everything that is available, you can take a look at this devhttp page.

In PHP, to read a users IP address, you would look at the specific key in the $_SERVER variable like this.
$ip_address = $_SERVER['REMOTE_ADDR'];
And here is how to do it in a Django view.
ip_address = request.META['REMOTE_ADDR']
That should get the ball rolling with meta information. If you have any other questions or suggestions, feel free to comment below! Enjoy!

Multiple Condition If Statements

Just a quick little note for when you are doing multiple condition if statements. Python includes AND and OR, but not && and ||. Although, in Python they are lowercase. This had me going for awhile. In PHP, some if statements.
if( $var1 > 50 && $var1 < 100 ){
echo 'hello world';
if( $var1 == 3 || $var1 == 7 ){
echo 'hello world';
And in Python.
if var1 > 50 and var1 < 100:
print('hello world')

if var1 == 3 or var1 == 7:
print('hello world')

Encoding A String With MD5

If you are to the point in your web app where you are ready to start dealing with user accounts and authentication, you may be thinking about cookies. Before you dive into any of this, please read chapter 14 of (all of it). It has tons of useful information about Django's built in account system that is already integrated with the Django Admin.

Now that we have that cleared up, lets get into some md5 encoding. Python offers a method pretty similar to PHP when it comes to md5. Simply include the hashlib library and you're good to go. Lets see how it would be done in PHP first.
$string = 'hello world';
$encoded_string = md5($string);
And here is the equivalent in Python.
import hashlib
string = 'hello world'
encoded_string = hashlib.md5(string).hexdigest()
Obviously, the import belongs at the top of your file, but you should know that. One key thing to keep in mind that hashlib is new in Python 2.5. In older versions, you would use md5.

Remember, simply using md5 does not mean a string will be secure. Don't forget to use a salt! If you don't know what that is, you can try reading something like this, or google it.

Thursday, February 18, 2010

Uppercase, Lowercase And Capitalized Strings

Often times you will need to convert the capitalization of text in a string. There are three commonly used PHP functions for achieving the basic conversions. These are strtoupper to convert a string to all uppercase characters, strtolower to convert a string to all lowercase characters and ucwords to capitalize the first letter of each word in a string.

In Python, you can use the .upper method, .lower method and .title methods respectively. Lets look at all three of these in an example. First, we will do it in PHP.
$string = "This is my STRING";
$uppercase = strtoupper($string); # THIS IS MY STRING
$lowercase = strtolower($string); # this is my string
$capitalized = ucwords($string); # This Is My STRING
And here it is in Python.
string = "This is my STRING"
uppercase = string.upper() # THIS IS MY STRING
lowercase = string.lower() # this is my string
capitalized = string.title() # This Is My String
As you can see, the first two produce the same results, but the third does not. Pythons .title method is actually equivalent to calling PHPs strtolower function on a string, before capitalizing the words.

If you have any comments, questions or suggestions for other topics, feel free to comment below. Enjoy!