1. EstablishDjango project:
Run: django-admin.py startproject hello
You can view the created project file:
_ Init _. py: indicates to the Python compiler that the content in the current folder is a Python project module.
Manage. py: The Python script file works with the Django command line tool django-admin.py to manage the configuration of the created Project.
Settings. py: the configuration file of the Django project. The project-related project module and global database configuration information are all set in settings. py.
Urls. py: configures URL address ing and manages URL address grids.
2. Create an APP:
In the django environment, a project can have many applications, and the applications under this project can share a set of environment settings.
Modify hello/settings. py
When the following attributes appear, modify them accordingly. The bold Section must be added. Optional values: Blue.
[Root @ htuidc hello] # vi hello/settings. py
Import OS
PROJECT_DIR = OS. path. dirname (_ file __)
'Engine ':'Sqlite3',
'Name ':'Hello. sqlite3',
Export age_code = 'zh-cn'
# This line is optional. If this field is set, create a static empty folder under hello.
MEDIA_ROOT = OS. path. join (PROJECT_DIR, 'static ')
# Add the following sentence in TEMPLATE_DIR = (
OS. path. join (PROJECT_DIR, 'template '),
# The following two sentences are added at the end of INSTALLED_APPS = (
# The admin page cannot be accessed without the following sentence
'Django. contrib. admin ',
'Hello. blog ',
# Use sqlite3 as the database to generate a hello. sqlite3 database to store data
DATABASES = {
'Default ':{
'Engine ': 'django. db. backends. sqlite3', # Add 'postgresql _ psycopg2 ', 'postgresql', 'mysql', 'sqlite3 'or 'oracle '.
'Name': 'Hello. sqlite3 ', # Or path to database file if using sqlite3.
'User': '', # Not used with sqlite3.
'Password': '', # Not used with sqlite3.
'Host': '', # Set to empty string for localhost. Not used with sqlite3.
'Port': '', # Set to empty string for default. Not used with sqlite3.
}
}
...
MEDIA_ROOT = OS. path. join (PROJECT_DIR, 'static ')
...
# Set the template directory:
TEMPLATE_DIRS = (
OS. path. join (PROJECT_DIR, 'template '),
)
) INSTALLED_APPS = (...
'Django. contrib. admin ',
'Hello. blog', # This is generated later. In fact, the app is created in the hello directory.
)
2. Create a blog app:
Run: python manage. py startapp blog
Add the model in hello/blog/models. py. This information provides the basis for django to create a database:
[Root @ htuidc hello] # vi blog/models. py
From django. db import models
From django. contrib import admin
# Create your models here.
Class Category (models. Model ):
Name = models. CharField (max_length = 32)
Def _ unicode _ (self ):
Return self. name
Class Admin:
Pass
Class Article (models. Model ):
Title = models. CharField (max_length = 64)
Published_at = models. DateTimeField ('date hhed ')
Content = models. TextField ()
Category = models. ForeignKey (Category)
Def _ unicode _ (self ):
Return self. title
Class Admin:
Pass
Admin. site. register (Category)
Admin. site. register (Article)
3. Create a database
Run: python manage. py SQL blog
Run: python manage. py syncdb
The latter will create a Manager account, set a password, etc.
4. Modify: hello/urls. py to remove some comments and add a url ing)
[Root @ htuidc hello] # vi hello/urls. py
From django. conf. urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
From django. contrib import admin
Admin. autodiscover ()
Urlpatterns = patterns ('',
# Examples:
# Url (R' ^ $ ', 'Hello. views. home', name = 'home '),
# Url (R' ^ hello/', include ('hello. foo. urls ')),
# Uncomment the admin/doc line below to enable admin documentation:
Url (R' ^ admin/doc/', include ('django. contrib. admindocs. urls ')),
# Uncomment the next line to enable the admin:
Url (r '^ admin/', include (admin. site. urls )),
(R' ^ blog/', include ('hello. blog. urls'), # Add ing
)
Create urls. py under hello/blog and enter the following code:
From django. conf. urls. defaults import *
From hello. blog. models import Article
Info_dict = {
'Queryset': Article. objects. all (),
}
Urlpatterns = patterns ('',
(R '^ $', 'django. views. generic. list_detail.object_list ', info_dict ),
(R' ^ (? P <object_id> \ d +)/$ ', 'django. views. generic. list_detail.object_detail', info_dict ),
)
5. Add management to the admin configuration page
The management block here refers to Category and Article
Open models. py under hello/blog
Add the following code:
From django. contrib import admin
Admin. site. register (Category)
Admin. site. register (Article)
Category and Article are exactly two classes that already exist with models. py. In this way, the management interface for the Category and Article modules will appear in the django background.
6. Run python manage. py runserver. Open http: // localhost: 8000/admin/in the browser and a front-end is available.
This article is from the galean blog, please be sure to keep this source http://galean.blog.51cto.com/7702012/1302749