The First Battle of Beleriand (python.Django)
In which the Elves under Elu Thingol rebuff the first assault of Morgoth’s Orcs, even in the hour of Feanor’s arrival in Middle Earth.
From last blog, we have prepared the environment (Python, Django and optional DB) for our first django web app project. In django, a project is defined to be “A collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.”
Before we start a new project, take care the workspace directory. For example, I change directory to my linux document directory.
$ pwd $ /home/username/Documents
Let’s start a new django project with the following command which will creat a directory mysite in my current directory Documents.
$ django-admin.py startproject mysite
“django-admin” is Django’s command-line utility for administrative tasks. It will be frequently used in the following discussion together with another command “manage.py”.
The usage is simple:
$ django-admin <command> [options]
For more commands and descriptions, please use the following command to check. The django official document also provides well-written description.
$ django-admin help
- The above command django-admin.py maybe a little different based on how you set up your environment. i.e, If you use pip to install django like me, them django-admin.py should already be in the linux system path.
- If you install django via some package stuff, then the command should be django-admin without extension .py. Moreover, prior to django 1.7, django-admin was only installed as django-admin.py. (My current version is 1.4.0, thus I should apply “.py” extension.)
- Additionally, in Windows environment, you may also need to set the environment avriable PATH (under Settings - Control Panel - System - Advanced - Environment…) to point to its installed location, i.e, add C:\Python34\;C:\Python34\Scripts; to your PATH.
- Actually, based on some of my experience, Windows is really not a suitable OS to do django, the process and setting is realatively complicated and confusing.
Let’s use tree command to take a look at the contents of directory mysite:
mysite/ ├── manage.py └── mysite ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
manage.py is a thin wrapper around django-admin that takes care of several things for you before delegating to django-admin. I am going to talk about the differences between manage.py and django-admin.py in a independent blog.
Now, we only need to know that, when working on a single Django project, it’s easier to use manage.py than django-admin. If you need to switch between multiple Django settings files, use django-admin with DJANGO_SETTINGS_MODULE or the –settings command line option.
The command-line examples using django-admin could also use manage.py just as well.
In outer mysite folder, there exists another inner mysite folder (“mysite/mysite/”). The outer mysite/ directory is just a container for your project. Its name doesn’t matter to Django, you can rename it to anything you like. 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.
In mysite/mysite/ folder, there are four python script files:
__init__.py: A file required for Python to treat the mysite directory as a package. It’s an empty file, and generally you won’t add anything to it.
settings.py: Settings/configuration for mysite project. Take a look at it to get an idea of the types of settings available, along with their default values.
urls.py: The URLs for this Django project. Think of this as the “table of contents” of your Django-powered site.
wsgi.py: An entry-point for WSGI-compatible webservers to serve your project. See How to deploy with WSGI for more details.
Actually, without any other actions, we already have a working django web application. But to run our mysite application, we need a Web server. Fortunately, django have considered this for us. Django has a built-in, lightweight Web server called “runserver” works as django development server.
In another word, we are not required to configure another third-party web server (i.e, Appach, Tomcat, IIS, etc), and thus could run our app rapidly.
To run the django development server, first change directory to mysite/ and then type the following command:
$ python manage.py runserver
When you see the following prompts, it means your server runs well:
$ python manage.py runserver Validating models... 0 errors found Django version 1.4, using settings 'mysite.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
This launches the server locally, on port 8000, accessible only to connections from your own computer. Click http://127.0.0.1:8000/ with your Web browser, you will see a webpage looks like this,
It maybe different based on different verions of django. This page is actually empty because we have not customized the our own webpage. Looking forward to that?