Web apps are often created using a framework. Frameworks make it easier to develop web apps that are scalable, reliable and maintainable. It avoids recreating the same code over and over again.
Common features are:
Security against common attacks
A framework may offer some or all of these features.
For example, the Flask web application framework does not have database support and you would need a separate module to use a database. The Django web application framework supports databases by default.
Why use a web framework?
As you are doing web development, you want to avoid spending time on programming things that have already been solved. On the other hand, if you are an experienced web developer a web framework may not offer everything you need.
What Python web frameworks exist?
Django and Flask are the most popular web frameworks. However, you may want to evaluate the frameworks. An overview:
The most popular python web application framework is Django, followed by Flask.
Django is the most used Python web framework. It takes care of many things so you can focus on the web app development. Sites built withDjango have dealt with high traffic spikes such as 50 thousands hits per second.
Database access is achieved through an Object-relational mapper: You define your data models in Python and Django deals with the actual database management systems (SQL). However, if you need to you can write your own SQL Queries with Django. URL routing is supported by Django. It encourages beautiful URL design such as ending without .php or .asp.
Flask is a Python micro framework which is modular by design. The framework is intended to build web apps. Flask does not have a specific database system or ORM system. If you want to use a database, you’ll have to use extensions. Flask is often combined with SQLAlchemy for database use.
Flask is very easy to get running, a minimal app would be:
Django code explanation:
The top lines import the Django library:
from django.confimport settings
from django.conf.urlsimport patterns
from django.httpimport HttpResponse
from django.core.managementimport execute_from_command_line
If you open the link /hello/, the webserver will call the index() function. We map the url to the function using:
urlpatterns = patterns('',(r'^hello/$', index),)
In Django we have url friendly urls. This means you do not have a url that ends in /id=1359835, but instead we have the directory as name. Finally, we set some default settings using settings.configure.
If you want to start with python web development, you could use a web framework named Django. It is designed to be fast, secure and scalable. It comes with an object-relational mapper (ORM), which means that objects in Python are mapped to objects in a database.
Applications created with Django are separated in three separate layers: model (database), view (appearance) and controller (logic), or shortly themodel-view-controller (MVC) architecture.
Pick from sqlite3, postgresql_psycopg2, mysql or oracle. Name is the name of your database. If you use SQLite the database will be created automatically. For MySQL and Postgresql you need to create the database yourself. Go up one directory to /mysite/ and run:
python manage.py runserver
The terminal should say:
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run'python manage.py migrate' to apply them.
August16,2015 - 14:45:29
Django version 1.7.1, using settings 'myapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[16/Aug/201514:45:35]"GET / HTTP/1.1"2001759