2013年9月21日星期六

Crusher Django Tutorial(1) hello world


if __name __ == "__main__":
    print "jaw crusher from China!"

Cute and hate ADs: A suck website I made(For god sake help me click it or just ignore it.55555555)

Hello,everyone,I will start a Django Tutorial,and I am a big Fan of Python and Django,So, I mean I am crush into Python and Django. I using the GNU/Linux Operating System to learning the Python and Django, and I think the Windows CMD is too weak to doing the Programming. :) Let us start the tutorial with the Terminal in the Linux. Let us try hello world with the terminal.

Then we install the Django(the install is so easy , and I skip the step). First,we check out the django lib is ok in your system:


Python 2.6.5 (r265:79063, Oct 1 2012, 22:07:21)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> import django
>>> dir(django)
['VERSION', '__builtins__', '__doc__', '__file__',
'__name__', '__package__', '__path__', 'get_version']
>>> django.get_version

>>> django.get_version()

Well,well,well, the django is installed in your System. Then we can just turn a directory that is English path(I am a Chinese),so maybe there is nothing not prefect with you. Here I use the root directory of my user ~ :


zoo@ubuntu:~$ django-admin startproject hello

nothing error, nothing means everything ok. And check the path, there is a folder Added named hello. All right, that is our base directory.

In the folder, there are four files

the __init__.py is a file that will make the folder as a packet as default.I do not know the more about it. LOL... the manage.py includes the all kinds of the command we can use.

The settings.py is all kinds of the setting of the project,like the database we use, the templates we set down and so on, we will talk about it later.

The url.py is all the url Configuration we use it to conf the views to the function.If you do not understand, just do it, the more you do, the more you will understand.

Ok, just start the “hello world” page! Just open the terminal and start the command:


zoo@ubuntu:~/hello$ python manage.py runserver
0.0.0.0:8000
Validating models...
0 errors found
Django version 1.1.1, using settings 'hello.settings'
Development server is running at
http://0.0.0.0:8000/
Quit the server with CONTROL-C.

the runserver means start the server, use django you won't have to use the server like Apache or Nignx,just django!BTW, the 0.0.0.0 means if you have a public IP Address, your friends not in your local network will see your page, that is real cool! Now you can open the Chrome or firefix and type the address:

http://localhost:8000

Ok, the default page you see here! Then we can just try the real hello world, but not just use the default page of django. OK. First, add the file names views.py:


#!/usr/bin/env python
#-*- coding:utf-8 -*-
from django.http import HttpResponse
def hello(request):
    return HttpResponse("""<html>
             <body>
             <h1>
              Hello Crusher
             </h1>
             </body>
             </html>""")

Then, we need to conf the url.py to make the view inflect the function: eidt the url.py like this:


#!/usr/bin/env python
#-*- coding:utf-8 -*-

from django.conf.urls.defaults import *
from views import *

urlpatterns = patterns('',
    (r'^$', hello),
)

the line of : from views import * means import the function to the current file:url.py the line : (r'^$', hello), means the bind nothing of the url to the function.That means you refresh the http://localhost:8000 you can see the webpage we created:

Now, we create the first django page, in fact we can make the page a little more complex: just add the html like this in the “”””””:


<html>

<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>Hello Django</title>
    <style type="text/css" media="screen">
    h1 {
        color: red;
    }
    li {
        color: gray;
    }
    </style>
</head>

<body>
    <h1>Hello World</h1>
    <h2>I like the following item:</h2>
    <ul>
        <li>
            Python
        </li>
        <li>
            Django
        </li>
        <li>
            Linux
        </li>
    </ul>
</body>

</html>


Ok, then we refresh the page, we can see the new page like this:

Well, that seems better than the just hello world page. Wait, the next tutorial we will see something more fun, just wait me to reblog!