(Turn) Chapter 2 of Django Learning: Getting Started with Django Quickly

Install Python 

Installing Django is very easy. Because Django can run in any environment that can run Python, it can be configured in a number of ways.
In this chapter we will try to cover several common Django installation scenarios.


Django is written in 100% pure Python code, so you need to install Python, Django requires Python or higher.
If you use Linux or MacOSX, you may already have Python installed
. Enter "python" at the command line or terminal. If a prompt similar to the following appears, it means that Python is already installed: 

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

 Otherwise, an error message "command not found" appears 

You can go to http://www.python.org/download/ to download the Python installation

Install Django
Install the official release version of Django
to http://www.djangoproject.com/download/ Download the tarball of Django-*.tar.gz
Java code copy code
tar xzvf Django-*.tar.gz
cd Django-*
sudo python setup.py install
Java code collection code
tar xzvf Django-*.tar.gz
cd Django-*
sudo python setup.py install

For installation under Windows, decompress Django-*.tar.gz directly and run python setup.py install.
After installation, you should be able to import django module Java code in the Python interactive environment.
Copy code
>>> import django
>>> django.VERSION
(2, 0, 3, 'final', 0)

The Python interactive environment is a command-line program, and you can enter the interactive environment by running "python" on the command line.
In this book, we will demonstrate some Python code examples that appear to be typed in the interactive environment.
The prompt in the Python interactive environment is three greater than signs (>>>)

Installing Django from Subversion
If you want to install the latest Django code or you want to contribute code to Django, you should install from Django's Subversion repository.
Subversion is an open source version control system similar to CVS that the Django team uses to manage changes to Django code.
You can always use the Subversion client to get the latest Django source code, or update the Django code in your local Subversion working copy "local checkout"
to get the latest changes and enhancements made by the Django developers.
The latest Django development code is called "the trunk"
to get the latest Django trunk:
1. Make sure you have the Subversion client installed. Download
the Subverion documentation at http://subversion.tigris.org http://svnbook.redbean. com
2, run the following command to get trunk "svn co http://code.djangoproject.com/svn/django/trunk django_src"
3, symlink django_src/django to make django in your Python site-packages directory, or update PYTHONPATH specifies it to install
from Subversion without running "python setup.py install"
Django trunk is frequently updated with bug fixes and feature additions, you may need to update it frequently Run "svn update"
in the django_src directory to update the code

The only prerequisite for setting up a database
Django is to have Python installed, but this book focuses on one of the many advantages Django is proud of, developing database-enabled Web sites
. So you need to install a database server to store your data
. If you just want to scratch the surface, you can Skip this step and start a project, but trust us: you'll end up with a database anyway, because
all assume you already have a database
Django 1.0 supports 5 database engines:
PostgreSQL (http:// www.postgresql.org/)
SQLite 3(http://www.sqlite.org/)
MySQL(http://www.mysql.com/)
Microsoft SQL Server(http://www.microsoft.com/sql/ )
Oracle (http://www.oracle.com/database/)
We ourselves especially like PostgreSQL, so we mentioned it first.
Still , all of these databases work just fine with Django and
SQLite deserves special attention , it's a very simple database engine that doesn't require any server installation and configuration
If you just want to play around with Django, SQLite is the easiest to install

Use PostgrSQL to work with Django
If you use PostgreSQL, you need the psycopg package, available from http://initd.org/projects/psycopg1 Make
sure you are using version 1 instead of version 2, 2 or beta
if you are on Windows PostgreSQL, you can download the compiled binary psycopg from
http://stickpeople.com/projects/python/win-psycopg/

Use SQLite 3 to work with Django
You need SQLite 3 not SQLite 2, download pysqlite from http://initd.org/tracker/pysqlite Make
sure to download pysqlite version 2.0.3 and above

Using MySQL to work with
Django Django requires MySQL version 4.0 and above, version 3.x does not support transactions, nested stored procedures, and other standard SQL statements
You also need the MySQLdb package, download from http://sourceforge.net/projects/mysql -python

Using MSSQL to work with Django

Using Oracle to work with Django

Don't use a database to work with Django
As just mentioned, Django doesn't actually need a database
If you just want Django to serve dynamic web pages without touching a database it
's fine to have some additional tools bundled with Django that require a database if you Choose not to use a database, you will miss those features

Start a project
If this is your first time using Django, you must be aware of some initialization procedures
Running "django-admin.py startproject mysite" will create a mysite directory in your current directory
Note that if you installed using setup.py Django, django-admin.py should be in your PATH system variable
If not in PATH, you can find it from site-packages/django/bin
Consider symlinking it to your PATH, e.g. /usr/local/bin
one A project is a collection of settings for a Django instance, including database configuration, Django-specific settings, and application-specific settings.
Let 's see what startproject creates:
/mysite/
     __init__.py
  manage.py
  settings.py
  urls.py
These The description of the file is as follows:
manage.py
a command line tool that lets you interact with Django projects in a variety of ways
setting.py
Django project configuration
urls.py Django
project URL definitions
Put it under the document root of the web server, such as /var/www
if you use Django don't do this, it is not a good idea to put Python code under the document root
This is not safe because people may see your code from the web
Put your code in a directory other than the document root, such as /home/mycode

Change the development server
to the mysite directory, run "python manage.py runserver", you will see the following information
Validating models...
0 errors found.
Django version 1.0, using settings 'mysite.settings'
Development server is running at http: //127.0.0.1:8000/
Quit the server with CONTROL-C.
This starts the Django development server, a lightweight web server included in Django for use during the development phase.
We include this in Django The server is for rapid development, so that you don't have to deal with
the configuration of the web server in production before the product goes live.
This server looks at your code, and if there are changes, it automatically reloads, allowing you to quickly modify your project without restarting.
Although this development server is great for development, please refrain from using it in any form Impulsivity in a production environment.
This server can only reliably handle one request at a time, and has not undergone any security checks at all.
If your site needs to go online, please refer to Chapter 21 for an introduction to deploying Django programs.
By default, the runserver command starts the server on port 8000 , only listen for local connections
If you want to change the port, add a command line parameter to
python manage.py runserver 8080
You can also change the IP address the server listens on, useful when you share a development site with other developers
python manage.py runserver 0.0.0.0:8080
The above command makes Django listen on any network interface, which allows other computers to connect The server
try to access http://127.0.0.1:8000/ and you will see the "Welcome to Django" page

What's next?
Now that we have everything installed and the server is running, let's write some basic code to show how to serve dynamic pages using Django

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325331524&siteId=291194637