This time, I used Django 1.4 and python 2.7. The main purpose of this article is to provide a good start for those who just learned django. We recommend that you refer to djangobook.
Our main task is as follows:
Enter localhost: 8000/currenttime/in the address bar. The current system time is displayed in the center of the page. Add a number after the url, the system time after this value is displayed on the page.
I will not talk about installing Django and python here. django can be installed using the easy_install tool. Note that the structure of django in 1.4 is slightly different from that in previous versions of django in the generated directory. It places the configuration files of this project, urlconfig, and wsgi in a folder, which is equivalent to an application, I think it aims to make the structure of the project directory more elegant.
Let's get started:
1, create a project using the django-admin.py startproject myTime
2. We do not need to use databases in this task, but we need to understand what the roles of each file at the beginning of the project are as follows:
_ Init _. py: Let Python treat this directory as a file required by an Development Kit (that is, a group of modules.
Manage. py: a command line tool that allows you to interact with this Django project in multiple ways.
Settings. py: settings or configurations of this Django project.
Urls. py: the url declaration of the Django project, that is, the content list of the Site Supported by Django
These are background things. For the foreground, we also need a view. py file to trigger different interface displays for specific URLs. Create a view. py in myTime.
3. We create a view function in the view. The Code is as follows:
From django. http import HttpResponse
From django. template. loader import get_template
From django. template import *
From django. shortcuts import render_to_response
Import datetime
Def current_datetime (request ):
Current_time = datetime. datetime. now ()
# T = get_template('first.html ')
# C = Context ({"current_time": now })
# Html = t. render (c)
# Return HttpResponse (html)
Return render_to_response('first.html ', locals ())
Def time_ahead (request, off ):
Current_time = datetime. datetime. now ()
Off = int (off)
Da = datetime. datetime. now () + datetime. timedelta (hours = off)
Return render_to_response ("child.html", locals ())
These two view functions are associated with the url. The html file is our Interface Template. For details about the template, see djangobook, too much content will be unclear at half past one. In fact, some code is added to html, which can be variables, function tags, or filters. And modify the template_dir in setting. py. Add the following code to it:
Import OS. path
ROOT_PATH = OS. path. dirname (_ file _)
Set template_dir
OS. path. join (ROOT_PATH, '../templates/'). replace ('\\','/'),
Note that the directory structure here is different from the previous version, so you need to add ..
4. Modify the url
From django. conf. urls import *
From mysite. view import *
Urlpatterns = patterns ('',
Url (r "^ currenttime/$", current_datetime ),
Url (r "^ currenttime/(\ d {1, 2})/", time_ahead ),
)
Note that the regular expression is used here. The symbols ^ and $ indicate the beginning and end of the expression respectively.
5. Set the template interface. The code for the two interfaces is:
First.html:
<Html>
<Head>
<Title>
My first template html page!
</Title>
</Head>
<Body>
<Center>
The time of now is {current_time }}
<Br/>
<Br/>
{% Block footer %}
This is my footer
{% Endblock %}
</Center>
</Body>
</Html>
The second interface is child.html:
{% Extends "first.html" %}
{% Block footer %}
This is my child footer
<Br/>
<Br/>
The real time ahead of {off }}is {da }}
{% Endblock %}
You can run the last one. We recommend that you refer to djangobook for details.