Django:

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
$ env/bin/activate
$ 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.

manage.py:

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.

myproject/__init__.py:

An empty file that tells Python, this directory should be considered a Python package.

myproject/settings.py:

Settings/configuration for this Django project. 

myproject/urls.py:

The URL declarations for Django project

myproject/wsgi.py:

An entry-point for WSGI-compatible web servers to serve your project.

Database setup:

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

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 file

Example:
  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:
Example:
    

    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

Example:myapp/urls.py

from django.conf.urls import patterns, url from polls import views

urlpatterns = 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:

myproject/urls.py

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.”

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties
Latest Comments
Related Articles
Implement search with Django-haystack and Elasticsearch Part-I Ravi kumar Gadila

Haystack works as search plugin for django. You can use different back ends Elastic-search, Whose, Sorl, Xapian to search objects. All backends work with same ...

Continue Reading...
Using Sentry To Track Django live Events Jagadeesh V

Let us now learn how to track exceptions and events in SENTRY. We will setup browser and server end tracking for Django Project.

Continue Reading...
Django permissions and Groups Ashwin Kumar

Django comes with a simple permissions system. It provides a way to assign permissions to specific users and groups of users. We can have permissions ...

Continue Reading...