Django is a high-level Python Web framework that encourages rapid development.
Install django in virtualenv to keep it safe from messing around with the other versions you may have.
$ virtualenv env
$ pip install django
If Django is installed, you can see the version of our installation by running following command
$ python -c "import django; print(django.get_version())"
otherwise you get an error “No module named django”
Creating a Project:
If this is your first time using Django, you’ll need to auto-generate code that establishes a Django Project
A collection of settings,
Including database configuration,
Django-specific options and application-specific settings.
With the following command:
$ django-admin.py startproject myproject
This will Create "myproject" directory in your current directory.The above command(startproject) creates
myproject/ |- manage.py |- myproject/ |- __init__.py |- settings.py |- urls.py |- wsgi.py
The outer "myproject/" root directory is where your project is stored.
It is automatically created in each Django project. manage.py is a thin wrapper around django-admin.py before delegating to django-admin.py.It puts your project’s package on sys.path,Sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file, Calls django.setup() to initialize various internals of Django.
The inner "myproject/" directory is the actual Python package for your project.
An empty file that tells Python, this directory should be considered a Python package.
Settings/configuration for this Django project.
The URL declarations for Django project
An entry-point for WSGI-compatible web servers to serve your project.
By default, SQLite is included in Python, so you won’t need to install anything to support your database.If you wish to use another database, install the appropriate database, and change ENGINE and NAME keys in the DATABASES to match your database connection settings
ENGINE – example: 'django.db.backends.sqlite3' NAME – The name of your database.
INSTALLED_APPS holds the names of all Django applications that are activated in this Django instance. By default, INSTALLED_APPS contains the following applications:
django.contrib.admin – The admin site. django.contrib.auth – An authentication system. django.contrib.contenttypes – A framework for content types. django.contrib.sessions – A session framework. django.contrib.messages – A messaging framework. django.contrib.staticfiles – A framework for managing static files.<.pre
Before using these applications you need to create a database by running the following command
$ python manage.py syncdb
The migrate command looks at the INSTALLED_APPS setting and creates necessary database tables according to the database settings in your myapplication/settings.py file.
Creating an App:
To create your app, make sure you’re in the same directory as manage.py and type this command:
$ python manage.py startapp myapp #This creates a directory myapp
myapp/ |-__init__.py |-admin.py |-models.py |-tests.py |-views.py
In our myapp application,create one Model: An Article has publication_date,title and content.
Edit the myapp/models.py fileExample:
from django.db import models class Article(models.Model): publication_date = models.DateField() title = models.CharField(max_length=50) content = models.TextField(max_length=200)
Activating models: First you need to tell your project that "myapp" app is installed.so,Include your app name(myapp) in INSTALLED_APPS of settings.py file
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', )
Now,run following command:
$ python manage.py syncdb
By running makemigrations, you’re telling Django that you’ve made some changes to your models. Now, run migrate again to create those model tables in your database:
For writting view. Open the file myapp/views.py and write your code:
from django.http import HttpResponse
def index(request): return HttpResponse("Hello,You're at the myapp index.")
To call the view, we need to map it to a URL we need a URLconf.To create a URLconf in the "myapp" directory, create a file called urls.py
Now your app looks like:
myapp/ |-__init__.py |-admin.py |-models.py |-tests.py |-urls.py |-views.py
from django.conf.urls import patterns, url from polls import viewsurlpatterns = patterns('myapp.views', url(r'^$', index, name='index'),)
The next step is to point the root URLconf at the myapp.urls module. In myproject/urls.py insert an include()Example:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('', url(r'^myapp/', include('myapp.urls')), url(r'^admin/', include(admin.site.urls)), )
The development server:
To start Django Development server change into your outer directory and run the following command
$ python manage.py runserver
Now Go to http://localhost:8000/myapp/ in your browser,you will see the text which you defined in the index view.
“Hello,You’re at the myapp index.”
Django-CRM :Customer relationship management based on Django
Django-blog-it : django blog with complete customization and ready to use with one click installer Edit
Django-webpacker : A django compressor tool
Django-MFA : Multi Factor Authentication
Docker-box : Web Interface to manage full blown docker containers and imagesMore...