Creating Django App

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.”
    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
How to create Custom User Model or Extend User Model in Django? Chaitanya Kattineni

Django provides built in authentication which is good for most of the cases, but you may have needs that are being served with the existing ...

Continue Reading...
How to customize Django Oscar Models, Views and URL's Chaitanya Kattineni

In order to customize models, views and urls, you need to fork an Oscar core app in which model/view resides. Then you should be able ...

Continue Reading...
Querying with Django Q objects Dinesh Deshmukh

Querying with Django Q objects: Q object encapsulates a SQL expression in a Python object that can be used in database-related operations. Using Q objects ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties