Python web frameworks are high-level tools used for the development of web applications, APIs and more. Frameworks facilitate easy and speedy development of dynamic and robust web resources. While many frameworks for Python exist, this article focuses on Django and web2py and their differences.
Django is notably one of the most popular Python web frameworks in existence. It is a high-level open source full-stack framework with a focus on rapid development and clean design, and it allows you to build dynamic, well-structured applications. It is also based on the Don’t Repeat Yourself (DRY) principle. This means exactly what it sounds like. The idea is that there is no point in writing the same code over and over, so the framework provides a lot of features to prevent this. This makes Django one of the fastest frameworks there is. Django is well known for its templating features, Object Relational Mapping (ORM) tool, Class-Based Views, Admin, Routing and REST frameworks. It includes a lightweight standalone web server for development and testing.
Web2py is a scalable, open source full-stack framework for Python which comes with its own web-based IDE. Web2py has no requirements for installation and configuration, has readability features for multiple protocols, and can run on Windows, Mac, Linux/Unix, Google App Engine, Amazon EC2, and any web hosting that supports Python 2.5+. Web2py is also backwards-compatible. Its key features include Role-Based Access Control (RBAC), Database Abstraction Layer (DAL) and support for internationalization. It’s worth noting that the design of web2py was inspired by Django.
Web2py’s mantra is “simple is better than complex” and makes it easy to write simple, clean code, while Django values “explicit over implicit” and gives developers more control over the design/process.
Database and server access, config.
- Django:Django can be run together with Apache, NGINX using WSGI, Gunicorn, or Cherokee. It also has the ability to use other WSGI-compliant web servers such as Bjoern. Django’s ORM is one of its key features. It’s more efficient for larger models than web2py’s DAL.
- Django uses the Object-Relational Mapper to map objects to database tables. The framework’s main databases are MySQL, PostgreSQL, SQLite, and Oracle. Django, however, does have a fork called django-nonrel, which supports NoSQL databases such as MongoDB.
- If Django-jython is installed, Django may be run with Jython on any Java EE application server, such as Tomcat or Jetty.
- Web2py: Web2py services requests to Apache, Lighttpd, Cherokee, NGINX and Hiawatha with its built-in Rocket Server. It can also service requests with pretty much any other web server out there using FastCGI, CGI, WSGI, mod_proxy or mod_python.
- Web2py comes with an API called the Database Abstraction Layer that maps Python objects into database objects such as queries and tables. The DAL allows you to specify a dialect for the database back-end, and then generates the SQL in real time. This means you don’t have to write SQL at all. For detailed instructions on implementing databases using the DAL, read the web2py DAL documentation.
- Web2py also supports SQLite, MSSQL, MySQL, and PostgreSQL for Windows. For Mac, however, it only supports SQLite. To use other database back-ends, web2py allows users to install the appropriate drive for the back-end in question. This means that popular databases such as FireBird, MongoDB, etc. can be used with web2py.
- Django: While Django does have its own Django Template language, the learning curve is pretty gentle, and it’s completely optional. Django also has template inheritance features. This means that every web app has a base template which can then be inherited by other pages. Template inheritance ensures that you maintain only one copy of your markup in the future, as opposed to multiple copies. Apart from being used to minimize repetition of HTML to structure pages, they can also be used to minimize code in application views. Django also allows you to create your own template tags.
- Web2py: In contrast, web2py has no special template language, and all controls and web templates are written in pure Python. They’re also very simple to understand. Once you have the basic idea of how to write templates as views, you can start writing code.
Libraries/tutorials and the learning curve
- Django: Django has been around since July 2005, so there are several tutorials, libraries, and over 2500 packages etc. available to you. Django also has extensive documentation that can be accessed here. For a comprehensive list of reusable apps, tools, etc., visit https://djangopackages.org. Django has a slightly steep learning curve. Because its entire system is based on inheritance, it can be quite complex for people who are new at Python or not very familiar with object-oriented programming in general.
- Web2py: Web2py was developed in 2008. While the framework has a very helpful and active society which provides free plugins, applications and a lot of tutorials, it still has significantly less support than Django. Yet this is mitigated by the fact that web2py’s overall learning curve is much easier in comparison to Django. Web2py was specifically designed as a learning tool and is astonishingly easy to set up. All that is required is that you download the web2py distribution, start up its built-in Rocket web server, and get to coding.
The question: What should you use, and when?
The philosophies of both frameworks are deeply rooted in Python, but there are some advantages to using one over the other in certain scenarios. If you’re a beginner programmer, or a newbie at web dev, web2py is definitely for you. However, if you’re fluent in Python and you need to meet a close deadline, Django would be the better option. Also, for popularity reasons, if you’re a freelancer looking to land web development jobs, learning to use Django would be more suited to your needs as there are more jobs available for it.