Write the first Django app, the first part -- create a project

Source: Internet
Author: User
Tags install django

Write the first Django app, the first part -- create a project

Let's use examples to learn.

Through this tutorial, we will teach you step by step to create a simple voting system.
This system is divided into two parts:
1. A public page allows people to vote and view the voting results.
2. An administrator page allows you to add, modify, and delete votes.

Assume that you have installed Django. You can run the python compiler and input import Django
To test whether Django has been installed. If the command runs successfully without errors, it indicates that Django has been installed.

Create a project

If you are using Django for the first time, make sure that some Initialization is complete. That is, you need to automatically generate
SomeCodeCreate a Django project-a series of Django configuration instances, including database configuration and Django-specific
Options andProgramSpecific settings.

Use the command line, CD enter the folder where you want to save your code, and run the command django-admin.py startproject mysite.
It creates a mysite folder in the current folder.

If you install Django through Python setup. py, The django-admin.py will be in your system path.
If it is not in your path, you can find it in site-packages/Django/bin. Site-packages is your python
Installation directory.
(I am not very familiar with the above, I copy the django-admin.py to the folder where the code is stored, and then run the command
Django-admin.py startproject mysite to create the project ).

Let's take a look at what startproject generates:
Mysite/
_ Init _. py
Manage. py
Settings. py
URLs. py
These files are:
1. _ init _. py: An empty file used to tell python to regard this folder as a python package. (If you are a beginner,
Read the official Python documentation to learn more about packages .)
2. Manage. py: A Practical command line that allows us to interact with Django projects in various ways.
3. settings. py: Set/configure the Django Project
4. URLs. py: URL ing of the Django project.

Development Server

Let's confirm these tasks. Switch to the mysite folder and run the python manage. py runserver command. You will see the command line
Output the following information.
Validating models...
0 errors found.

Django Version 1.0, using settings 'mysite. setting'
Development server is running at http: // 127.0.0.1: 8000/
Quit the server with CONTROL-C.

You have started the Django development server, a lightweight Web Server written in pure python. We have included it in djangok,
So you can quickly develop projects without configuring product services, such as Apache, until you are about to release the product.

This is a good time to note: Do not use this server as the running environment of the product. It is only used during development.
(Our business is to develop Web frameworks rather than Web servers .)
Now that the server is running, use your browser to browse http: // 127.0.0.1: 8000 /.
You will see a bright blue crayons page of "Welcome to Django. It already works.

Change its port
By default, the runserver command starts the development server on port 8000. If you want to change the server port, use the port number
Input command line parameters. For example, this command starts the server on port 8080:
Python manage. py runserver 8080

(On my computer, if you use Python manage. py runserver to start the server, an error is reported. Remove the python before the command.
It can run normally. I don't know what is going on .)

Prepare Database

Now, edit settings. py. It is a common Python module that contains module-level variables corresponding to Django settings.
Modify these settings to match your database connection parameters:
1. database_engine -- one of 'postgresql _ psycopg2 ', 'mysqlite3,
Others also apply.
2. database_name -- database name. If you are using SQLite, the database is a file on your computer,
Database_name is the complete absolute path of the file, including the file name. If the file does not exist, it will use the database for the first time.
.
Use a slash when specifying the file path, even in Windows (such as C:/homes/user/mysite/sqlite3.db ).
3. database_user -- User Name of your database (not used by SQLite ).
4. database_password -- password of your database (not used by SQLite ).
5. database_host: Host of the database. If your database is on the same physical machine, keep it as a null string (SQLite is not used ).

If you are a beginner in database, we recommend that you use simple SQLite (set database_engine to 'sqlite3 ').
SQLite is part of python2.5 and the updated version, so you do not need to install anything.
If you are using PostgreSQL or MySQL, make sure that you have created a database. You can create a database in the database interaction dialog box.
Run the "Create Database database_name;" command ;".
If you are using SQLite, you do not need to create anything in advance-The database will be automatically created when it is needed.

When editing settings. py, pay attention to the following installed_apps settings. Multiple variables are used to save all activated Django applications.
The name of the Django instance of the program. Apps can be used in multiple projects. You can package and publish them for use in other projects.

By default, installed_apps includes the following apps, all of which are from Django:
1. Django. contrib. auth -- user verification system.
2. Django. contrib. contenttypes -- content type framework.
3. Django. contrib. sessions -- session framework.
4. Django. contrib. sites-use a Django framework to manage multiple sites.
By default, these applications are included for easy use.

Each application needs to use at least one data table, because we need to create a data table before using them. 0
To do this, run the following command:
Python manage. py syncdb

The syncdb command searches for nstalled_apps settings in your settings. py file and uses database settings to create the required data table.
You will see the creation information of each data table, And if you create a Super User Account for the verification system, you will see a dialog box.
Continue and complete it.

If you are interested, run the command line of your database client, enter \ DT (PostgreSQL), show tables; (MySQL ),
Or. Schema (SQLite) to display the table created by Django.

As mentioned above, the default applications include these for general needs, but not everyone needs to use them.
If you don't need them, you can comment them out or delete them from installed_apps, but before you run the syncdb command.
The syncdb command only creates a table for apps in installed_apps.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.