A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Category Archives: General

Python: Saving a file from the URL by chunks

For example, when you want to save an image from a URL. https://github.com/images/modules/header/logov3-hover.png f = urllib.urlopen(request.POST[’url’]) p = os.path.join(settings.UPLOAD_DIRECTORY, "saved_file.jpg") filesize = 0 with open(p,’wb’) as output: while True: buf = f.read(65536) filesize += len(buf) if not buf: break output.write(buf)

Posted in General | Tagged | Leave a comment

Django: Reading an image outside the public accessible directory and displaying the image through the browser

In some cases you would want to have “private” images for your logged-in users, you will not be able to store the images in the public accessible directory. For example, if you have “my_private_photo.png” stored in your normal “images” directory, … Continue reading

Posted in General | Tagged , , | 1 Comment

Apache mod_xsendfile for my Django app (UPDATED)

Here is how to setup/install mod_xsendfile in Apache2 + Django (mod_wsgi). First do this to install apxs2 $> sudo apt-get install apache2-prefork-dev Download mod_xsendfile: http://tn123.ath.cx/mod_xsendfile/ Install it by doing this: $> apxs2 -cia mod_xsendfile.c If you get an error compiling … Continue reading

Posted in General | Tagged , , , | 2 Comments

File Streaming in Django (Sending large files through Django)

I am planning to build something involving serving large files over HTTP. Putting the files in a public accessible directory is not an option since I need to monitor the users who download the file. Users will need to login … Continue reading

Posted in General | Tagged , , , | 3 Comments

A Simple Tkinter Example (Python)

What is Tkinter? Tkinter is Python’s de-facto standard GUI (Graphical User Interface) package. It is a thin object-oriented layer on top of Tcl/Tk. Here is a simple Tkinter example. The code below should generate a very simple GUI with a … Continue reading

Posted in General | Tagged , | 2 Comments

A Basic Python Class

This is the basic structure of a Python class. >>> class Simplex: … "An example class""" … def __init__(self, x, y): … self.a = x … self.b = y … def addition(self): … return self.a + self.b … >>> z … Continue reading

Posted in General | Tagged , , , | 1 Comment

My notes for Lists in Python

Lists A list is a sequence of values. [ and ] like numerically indexed arrays in PHP >>> # a list of numbers >>> my_numbers = [10, 15, 20, 25] >>> print my_numbers [10, 15, 20, 25] >>> >>> #a … Continue reading

Posted in General | Tagged , | 2 Comments

Basic Python: Filenames and paths

Another Python post! I am still learning the language. Since I cannot remember everything, I am posting it here for future reference. Get the Current Working Directory To get the “current working directory” do this: import os current_dir = os.getcwd() … Continue reading

Posted in General | Tagged , , , , , , , | Leave a comment

Django and jQuery AJAX

This is probably my first post for Django. I have only been using it for a few weeks and I am already falling in love with it. Here is how to do an AJAX “save” using jQuery and Django. My … Continue reading

Posted in General | Tagged , , , | Leave a comment

WordPress: How to enable widgets in your custom theme

This is probably in the WordPress documentation but to enable widgets in your own customized theme, do this: … rest of PHP and HTML code in your here … <div id="sidebar"> <ul> <?php if ( ! dynamic_sidebar( ‘primary-widget-area’ ) ) … Continue reading

Posted in General | Leave a comment