Chinaunix首页 | 论坛 | 博客
  • 博客访问: 855864
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: 系统运维

2011-09-14 14:43:28

Creating a project
If this is your first time using Django, you’ll have to take care of some initial setup. Namely, you’ll need to auto-generate some code that establishes a Django project – a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.

From the command line, cd into a directory where you’d like to store your code, then run the following command:

django-admin startproject prj
it will create a prj directory in the current directory.there are some files in the prj directory:
[root@localhost prj]# ls
__init__.py  manage.py  settings.py  urls.py

These files are:

  • __init__.py: An empty file that tells Python that this directory should be considered a Python package. (Read in the official Python docs if you're a Python beginner.)
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in .
  • settings.py: Settings/configuration for this Django project. will tell you all about how settings work.
  • urls.py: The URL declarations for this Django project; a "table of contents" of your Django-powered site. You can read more about URLs in .

The development server

Let's verify this worked. Change into the mysite directory, if you haven't already, and run the command python manage.py runserver. You'll see the following output on the command line:

Validating models... 0 errors found. Django version 1.0, using settings 'prj.settings' Development server is running at Quit the server with CONTROL-C.

You've started the Django development server, a lightweight Web server written purely in Python. We've included this with Django so you can develop things rapidly, without having to deal with configuring a production server -- such as Apache -- until you're ready for production.

Now's a good time to note: DON'T use this server in anything resembling a production environment. It's intended only for use while developing. (We're in the business of making Web frameworks, not Web servers.)

Now that the server's running, visit with your Web browser. You'll see a "Welcome to Django" page, in pleasant, light-blue pastel. It worked!


By default, the command starts the development server on the internal IP at port 8000.

If you want to change the server's port, pass it as a command-line argument. For instance, this command starts the server on port 8080:

python manage.py runserver 8080

If you want to change the server's IP, pass it along with the port. So to listen on all public IPs (useful if you want to show off your work on other computers), use:

python manage.py runserver 0.0.0.0:8000

Full docs for the development server can be found in the reference.


Database setup

Now, edit settings.py. It's a normal Python module with module-level variables representing Django settings. Change the following keys in the 'default' item to match your databases connection settings.

  • -- Either 'django.db.backends.postgresql_psycopg2', 'django.db.backends.mysql' or 'django.db.backends.sqlite3'. Other backends are .

  • -- The name of your database. If you're using SQLite, the database will be a file on your computer; in that case, should be the full absolute path, including filename, of that file. If the file doesn't exist, it will automatically be created when you synchronize the database for the first time (see below).

    When specifying the path, always use forward slashes, even on Windows (e.g. C:/homes/user/mysite/sqlite3.db).

  • -- Your database username (not used for SQLite).

  • -- Your database password (not used for SQLite).

  • -- The host your database is on. Leave this as an empty string if your database server is on the same physical machine (not used for SQLite).

If you're new to databases, we recommend simply using SQLite (by setting ENGINE to 'django.db.backends.sqlite3'). SQLite is included as part of Python 2.5 and later, so you won't need to install anything else.

Note

If you're using PostgreSQL or MySQL, make sure you've created a database by this point. Do that with "CREATE DATABASE database_name;" within your database's interactive prompt.

If you're using SQLite, you don't need to create anything beforehand - the database file will be created automatically when it is needed.


While you're editing settings.py, take note of the setting towards the bottom of the file. That variable holds the names of all Django applications that are activated in this Django instance. Apps can be used in multiple projects, and you can package and distribute them for use by others in their projects.

By default, contains the following apps, all of which come with Django:

  • -- An authentication system.
  • -- A framework for content types.
  • -- A session framework.
  • -- A framework for managing multiple sites with one Django installation.
  • -- A messaging framework.
  • -- A framework for managing static files.

These applications are included by default as a convenience for the common case.

Each of these applications makes use of at least one database table, though, so we need to create the tables in the database before we can use them. To do that, run the following command:

python manage.py syncdb

The command looks at the setting and creates any necessary database tables according to the database settings in your settings.py file. You'll see a message for each database table it creates, and you'll get a prompt asking you if you'd like to create a superuser account for the authentication system. Go ahead and do that.

If you're interested, run the command-line client for your database and type \dt (PostgreSQL), SHOW TABLES; (MySQL), or .schema (SQLite) to display the tables Django created.

For the minimalists

Like we said above, the default applications are included for the common case, but not everybody needs them. If you don't need any or all of them, feel free to comment-out or delete the appropriate line(s) from before running . The command will only create tables for apps in .

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

+----------------------------+
| Tables_in_django           |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_message               |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_content_type        |
| django_session             |
| django_site                |
+----------------------------+




阅读(483) | 评论(0) | 转发(0) |
0

上一篇:文件操作

下一篇:create models

给主人留下些什么吧!~~