Creating A Django Project

The first step is to start a new Django project. Basically, this means that we'll run some scripts provided by Django that will create the skeleton of a Django project for us.

This is just a bunch of directories and files that we will use later.In your MacOS or Linux console you should run the following command; don't forget to add the period (or dot) . at the end:(The period . is crucial because it tells the script to install Django in your current directory (for which the period . is a short-hand reference))


(myvenv) ~/djangoproject$ django-admin startproject mysite .


Note:Remember to run everything in the virtualenv. If you don't see a prefix (myvenv) in your console you need to activate your virtualenv.


Creating a project


django-admin startproject mysite

django-admin.py is a script that will create the directories and files for you. You should now have a directory structure which looks like this:
Let’s look at what startproject created:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py


These files are:

    The outer mysite/ root directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
   
    manage.py: A command-line utility that lets you interact with this Django project in various ways.  a script that helps with management of the site. With it we will be able to start a web server on our computer without installing anything else,

    The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).
  
    mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. (Read more about packages in the official Python docs if you’re a Python beginner.)
   
    mysite/settings.py: Settings/configuration for this Django project.
  
    mysite/urls.py: contains a list of patterns used by urlresolver.
The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.

   
    mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project



Running the Server

Let’s verify your Django project works. Change into the outer mysite directory, if you haven’t already, and run the following commands:

$ python manage.py runserver
or
./manage.py runserver
(Default port 8000)

$ python manage.py runserver 8080
./manage.py runserver  (don\'t need to prefix python  )

$ python manage.py runserver 0.0.0.0:8000  (Toi make it public)



Changing settings
 
Let's make some changes in mysite/settings.py

It would be nice to have the correct time on our website. Go to the wikipedia timezones list and copy your relevant time zone (TZ). (eg. Europe/Berlin )

In settings.py, find the line that contains TIME_ZONE and modify it to choose your own timezone:

TIME_ZONE = 'Europe/Berlin'

We'll also need to add a path for static files
Go down to the end of the file, and just underneath the STATIC_URL entry, add a new one called STATIC_ROOT:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')



Setup a database

We'll use the default one, sqlite3.

This is already set up in this part of your mysite/settings.py file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

To create a database for our blog, let's run the following in the console:
 python manage.py migrate 

(we need to be in the djangoprojects directory that contains the manage.py file

Starting the Server


And we're done! Time to start the web server and see if our website is working!

You need to be in the directory that contains the manage.py file (the djangoprojects directory). In the console, we can start the web server by running python manage.py runserver:

(myvenv) ~/djangoprojects$ python manage.py runserver

http://127.0.0.1:8000/




Congratulations! You've just created your first website and run it using a web server!

0 comments:

Copyright © 2013 SoftKul