Category Archives: Web Development

Tutorial: Your first WSGI Python web application served on Apache HTTPd.

Abstract

We’ll create a new python project using wsgi (web server gateway interface)

 

Introduction

Python based web applications can be served in several ways. You can use the old and good CGI interface, or use the more common used mod_python. FastCGI and SCGI are also an option. but most of those ways supports the WSGI interface which is the recommended way. You can learn more about each way, and the cons and pros of each, and what is wsgi  in this great tutorial/article: How to use python in the web.

We’ll build a skeleton for a Python based WSGI web application hosted on Apache HTTPd. I’ve choosed WSGI over other methods to promote web application portability across a variety of web servers.

 

Prerequisities

  • Python
  • Apache HTTPd Service running and working. trim instance is better.

 

Setup

mod_wsgi

red-hat/fedora/centos: install mod_wsgi using:

yum install mod_wsgi

debain/ubuntu: install mod_wsgi using:

apt-get install mod_wsgi

 

Virtual Application

configure your httpd with:

1
2
3
4
5
WSGIScriptAlias /myapp /var/www/myapp/myapp.wsgi
<Directory /var/www/myapp>
   Order allow,deny
   Allow from all
</Directory>
WSGIScriptAlias /myapp /var/www/myapp/myapp.wsgi
<Directory /var/www/myapp>
   Order allow,deny
   Allow from all
</Directory>

 

WSGI

Create the file myapp.wsgi at ‘/var/www/myapp’ and fill it with the example from mod_wsgi documentation:

1
2
3
4
5
6
7
8
9
def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'
 
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
 
    return [output]
def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

You may need to restart your apache using: service httpd restart.

Now use your browser to see the ‘Hello World’ response from your first wsgi server.

Helloworld

 

More information

 

Happy wsgi python-ing…!

CodeLobster Handy Code Tools Review – Free portable IDE for PHP/HTML/CSS/JavaScript development

Overview

Excited today, I’m going to try a new IDE for Windows: CodeLobster PHP Edition 4.5.1.

With almost 30 years experience in development, from the age of Commodore & QBasic to today’s VS, Eclipse, NetBeans, Vi, etc….  I’ve used them all to create some code (even  NotePad). Sometimes, I am using more then one editor at once!

 

Continue reading

Fix WordPress Plugin HTML5 Swiffy Insert

Google Swiffy

Swiffy is a web-based tool developed by Google that converts SWF files to HTML5.
Its main goal is to display Flash contents on devices that do not support Flash, such as iPhone, iPad, and Android Tablets.

Continue reading

Django URL dispatcher routing beginners tutorial

How to use Django URL dispatcher

Django routing module called URL dispatcher

url.py

Normally, When a user requests a page from your Django-powered site Django loads the URLconf  module from a file called urls.py at the root of your project, Ordinarily, this is the value of the ROOT_URLCONF setting, The URL dispatcher can be overridden when the incoming HttpRequest object has an attribute called urlconf (set by middleware request processing), its value will be used in place.

First line in your urls.py should be:

1
from django.conf.urls import patterns

 

urlpatterns

Django loads that file and looks for the variable urlpatterns. This should be a Python list, in the format returned by the function django.conf.urls.patterns().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from django.conf.urls.defaults import patterns

urlpatterns = patterns('',
    # ex: /
    (r'^$', 'myapp.views.home', {}, 'index'),
    # ex: /about
    (r'^about', 'myapp.views.about', {}, 'about'),
    ....
)

# To add more urlpatterns in the same urls.py you can use:
urlpatterns += patterns('',
    ....
)

URL Pattern Arguments

  1. First Argument is URL pattern.
  2. Second is the View to send back.
  3. Third is Dictionary to send to the view.
  4. Last argument is Name. Naming enables calling {% url name %} from templates.

Notes

  • Django runs through each line, in order, and stops at the first one that matches the request.
  • The ‘r’ in front of each regular expression string is optional but recommended. It tells Python that a string is “raw” – that nothing in the string should be escaped. See Dive Into Python’s explanation.

url() function

You can use the url( convenience wrapper.

The url() function is passed five arguments, two required: regex and view, and three optional: kwargsname, and prefix.

url(regex, view, kwargs=None, name=None, prefix=”)

You can use the url() function, instead of a tuple, as an argument topatterns(). This is convenient if you want to supply a name but not extra_context you’d still have to include an empty optional extra arguments dictionary in the tuple version

Remember you need to include the url, from django.conf.urls.defaults.

1
from django.conf.urls.defaults import patterns, url

These are the same:

1
2
(r'^$', 'views.home', {}, 'index'),
url(r'^$', views.index, name='index'),

 

Import views to shorten code

You can use either way, as you feel convenient for you. It’s the same code above, but using Import to shorten code:

Method 1: Import Views Class

1
2
3
4
5
6
7
from django.conf.urls.defaults import patterns
from myapp import views

urlpatterns = patterns('',
    (r'^$', 'views.home', {}, 'index'),
    .....
)

Method 2: Import Views (or *)

1
2
3
4
5
6
from django.conf.urls.defaults import patterns
from myapp.views import home

urlpatterns = patterns('',
    (r'^$', 'home', {}, 'index'),
    .....

Method 3: Use Default Pattern

1
2
3
4
urlpatterns = patterns('myapp.views',
    (r'^$', 'home', {}, 'index'),
    .....
)

 

Include

You can include from another file, Django app or object. remember to import the include() function.

1
2
3
4
5
6
from django.conf.urls.defaults import patterns, url, include
urlpatterns = patterns('',
    # ex: /help
    url(r'^help/', include('help.urls')),
    .....
)

This code will search for url.py file inside help app in your django project. construct the file the same as the url.py file.

 

Include in the same file

1
2
3
4
5
6
7
8
9
10
from django.conf.urls.defaults import patterns, url, include

extra_patterns = patterns('',
    .......
)

urlpatterns = patterns('',
    (r'^extra/', include(extra_patterns)),
    .....
)

 

 

These are the URL dispatcher basics. Maybe I’ll be post later another post on more advanced aspects of the URL dispatcher include URL grouping, passing extra parameters to view and more…

CYA,

And good luck with Django – It worth it!

Develop with the new WordPress 3.5 Media Manager.

Overview

If you are a WordPress plugin developer, you may want to use the new build-in WordPress 3.5 Media Manager in your plugin. This tutorial will teach you how to implement the Media-Manager in your plugin.

Continue reading

Windows 8: How to add/pin your website tile to start menu (meta tags)

 Overview

You will learn how to add necessary META your website as a tile to support windows 8 tile start menu pin using IE 10. Just like windows 8 application do!.

Continue reading

Using modern.IE – Testing for Internet Explorer just got a little easier

internet explorer logo

Microsoft to developers: This is the ‘modern.IE’ world

Microsoft announced their new tool called modern.IE. http://www.modern.ie/
Using this tool (website) you can scan for common coding problems.

Continue reading

Fix: WordPress 3.5 Update Mess-up system (JScript Issue)

Introduction

Recent WordPress Update to Version 3.5 has made problem in many of our wordpress systems.
Options like: Widgets Drag & Drop, Add Media, Screen Options, Some options at TinyMCE such as Insert/Edit Link, Visual/Text and more not working anymore.

Continue reading