Cute and hate ADs: A website I made for sale impact-crusher(For god sake help me click it or just ignore it.55555555)
In the previous post,I am sorry make the code so ugly that maybe you can not understand,thanks for @Leo Trubach,he gave me a lot of tips about the code style.BTW, he think the django version 1.1.1 is too old.Well,I chang the version to the Django-1.4.8 :), and I will show you how to install django,and then we will see something real with Django, hope you will like it!
Install django, first download django from here https://www.djangoproject.com/ I use the Version of 1.4.8, you can choice the version you like.
Then, use the tar command to decompress the packet like this
zoo@ubuntu:~/django$ tar zxvf Django-1.4.8.tar.gz
get in the directory, and use "sudo python" command to install Django like this
sudo python setup.py install
After input the password and Enter, it will get out a lot of install Information when it over,we can check it ok or not:
zoo@ubuntu:~/django/Django-1.4.8$ python
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()
'1.4.8'
When you see this, that means you have installed Django Successfully.Ok, we can move on about tutorial today. In this Tutorial,we will learn the basic url conf, which is different with original PHP(means not use the frame work).
Well, let continue the previous post,Oh, forget it,let us start from the very begining.
We just start a project with the command
zoo@ubuntu:~$ django-admin startproject hello
Then, we just create a Python file named views.py to create our basic function.
And now,we will use something Dynamic in the page to show the current time of your system.just edit the views.py like this:(we will talk about it later)
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from django.http import HttpResponse
import datetime
def time_now(request):
now = datetime.datetime.now()
html = "<html><body>It is %s now</body></html>" % now
return HttpResponse(html)
The code above here looks like the "Hello World" we talked before, just reedit the urls.py, set the homepage in the time_now function, you will get the result:
You may think that Django is so so, I can not get anything different like PHP.I have to write the code in the HTML and then give it to Django.Here we change something to make it better.
Here is a problem to hack.I have a number in a url, And I want to judge it is an odd number or even.The number input from the URL(that sounds silly),but we just assume that.Just for learn the url conf in Django. Just edit the views.py like this:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from django.http import HttpResponse,Http404
import datetime
def time_now(request):
now = datetime.datetime.now()
html = "<html><body>It is %s now</body></html>" % now
return HttpResponse(html)
def judge(reqest, number):
try:
number = int(number)
except ValueError:
raise Http404() # raise 404 error
if number % 2 == 0:
html = "<html><body>%d is even!</body></html>" % (number)
else:
html = "<html><body>%d is odd!</body></html>" % (number)
return HttpResponse(html)
That just add a more function, but we have a more parameter,that is from the URL in fact.Now we need to conf the URL Just edit the urls.py:
from django.conf.urls.defaults import *
from views import *
urlpatterns = patterns('',
(r'^$', time_now),
(r'^number/(.+)$', judge), # we just want the number in the url, not more or less
)
The (.+) means more than one chars, I know it is silly, if you know something better, have to tell me!!! Now, we can test it with the url: http://0.0.0.0:8000/number/123 or http://0.0.0.0:8000/number/-123 or http://0.0.0.0:8000/number/12 and so on.
Well, at least it works, but if we input nothing just like: http://0.0.0.0:8000/number/ the program will die.How to fix it? I use the StackOverflow haha: http://stackoverflow.com/questions/18943564/django-to-conf-a-url-that-contains-a-number/18943653#18943653
just add a more line to conf, even do not have to add more function! So cool!
from django.conf.urls.defaults import *
from views import *
urlpatterns = patterns('',
(r'^$', time_now),
(r'^number/(.+)$', judge), # we just want the number in the url, not more or less
(r'^number/$', judge), # handle the empty input
)
the function is :
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from django.http import HttpResponse,Http404
import datetime
def time_now(request):
now = datetime.datetime.now()
html = "<html><body>It is %s now</body></html>" % now
return HttpResponse(html)
def judge(reqest, number = None):
if number is None:
html = "nothing input!"
return HttpResponse(html)
try:
number = int(number)
except ValueError:
raise Http404()
if number % 2 == 0:
html = "<html><body>%d is even!</body></html>" % (number)
else:
html = "<html><body>%d is odd!</body></html>" % (number)
return HttpResponse(html)
That will works for our funtion! Well,Thanks for reading the second Tutorial, I am also new to Django, but I want to learn it. Just learn with me or read the book called: The Definitive Guide to django which is awesome!
没有评论:
发表评论