Remember the process of creating a Django app on Linux

Source: Internet
Author: User
Tags md5 postgresql psql install django virtualenv

0 Environment and goals0.1 environment
    • Server-side
      • Operating system: Ubuntu 14.04.2 LTS
      • Run Time: Python 3.4.0
    • Client Side
      • Browser: Firefox 37.0.1
      • Database client: Navicat Premium 11.1.10
0.2 goals

Start a Django project on the server side, using the PostgreSQL database.

0.3 points
    • Django Installation
    • PostgreSQL Installation
    • Django initializes the database
    • Start the Django Service
1 implementation1.1 Django Preparation1.1.1 Installing Django

Install the latest Django here

$sudo pip3 install django

If you are upgrading to install Django on an existing basis, enter the command

$sudo pip3 install django --upgrade

Pip will uninstall the old version of Django and install the latest version.
Note that the command used here is not sudo pip3 simple to use pip , because it is not installed in the virtualenv environment, all operations are done in the operating system environment, and Ubuntu default python environment is Python 2, many departments Basic software is based on this version of Python development, forced to the default use of Python 3 will encounter a variety of egg pain situation (do not ask me why I know). The reason for this is that this is just a project that I personally use to practice, no production significance, and laziness does not strictly isolate control. In the production of development and deployment, often have multiple versions of concurrent use of the need, according to the actual situation to configure the development and production environment, there is no detailed discussion.

1.1.2 Creating a Django Project

Here we first create a project named Teamtea .

$cd workspace/django$django-admin starproject teamtea

Yes, you can now use Django's engineering management commands directly django-admin .
This will be done after the project has been created and then set aside for later configuration.

1.2 PostgreSQL Preparation1.2.1 Installing PostgreSQL

Ubuntu official source PostgreSQL will always slow down a small version, version control is not pleasant, and then follow the official website instructions to install the latest version.
First add the PostgreSQL official source to the APT source list. Create a file in the /etc/apt/source.list.d/ directory pgdg.list with the file content

deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main

This is the source for Ubuntu 14.04, and other versions of the system can find the relevant source in the document.
Then import the signature of the source.

$wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

The official source of PostgreSQL is now added to the source list. Next, update the package list, install the required postgresql-9.4 and postgresql-server-dev-9.4 two packages.

$sudo apt-get update$sudo apt-get install postgresql-9.4 postgresql-server-dev-9.4

This completes the basic installation of PostgreSQL 9.4.
It is also important to note that the database version selection here is purely for personal preference, and should be selected on demand in the actual development and production process.

1.2.2 Configuring access rights

After the installation of PostgreSQL will automatically create a user named Postgres in the system, that is PostgreSQL administrator user, the operation of PostgreSQL is best done by this user, try to avoid using root user. In order to use this user, you first need to change its password.

$sudo passwd postgres

Postgres User Password and access

The above is the operation of the postgres user in the operating system, and we know that the database management system also has a user concept, PostgreSQL Administrator user is also postgres, in order to be able to use it to manage the database, also need to set the password. First log in to the postgres user,

$su postgres

Then go to the PostgreSQL hypervisor psql , which does not require a password, because the access control entry defaults to peer allow only postgres users on the local operating system to access.

$psql

At this point, the command prompt becomes postgres=# , stating that the environment is entered, the psql password is set

postgres=# \password

Then exit psql the environment,

postgres=# \q

Then modify the access control entries for the postgres user, edit the /etc/postgresql/9.4/main/pg_hba.conf file, and

local   all             postgres                                peer

Revision changed to

local   all             postgres                                md5

Reload the configuration file for the changes to take effect,

$pg_ctlcluster 9.4 main reload

Note that the Debian Linux system uses pg_ctlcluster commands rather than pg_ctl commands.
You will need to psql enter the password again when you try again.

In order to enable the client's Navicat login management PostgreSQL, you need to turn on the network port monitoring, edit the /etc/postgresql/9.4/main/postgresql.conf file, will

Revision changed to

Note that this removes the comment from the beginning of the line. Again, reload the configuration file for the changes to take effect.

1.2.3 Creating an app user and database

It is now possible to manage the PostgreSQL database through the Navicat connection, which makes it easy to create users and databases in a graphical interface, where you can create a user and database with the same name as the application, all named teamtea .

Also need to add access permissions, edit the /etc/postgresql/9.4/main/pg_hba.conf file, add a row

local   teamtea             teamtea                                md5

This restricts the user Teamtea to access only the database named Teamtea , which is sufficient for the application and is also secure for the entire database system.
Once you have done this, you can exit the postgres user.

The following is a draft

1.3 Configuring Django to start the service1.3.1 Configuring Django

Now you can start configuring our Django project Teamtea, go to the engineering catalog, edit the file /teamtea/settings.py file, modify the database configuration item to

DATABASES = {    ‘default‘: {        ‘ENGINE‘: ‘django.db.backends.postgresql_psycopg2‘,        ‘NAME‘: ‘teamtea‘,        ‘USER‘: ‘teamtea‘,        ‘PASSWORD‘: ‘teamtea‘,        ‘HOST‘: ‘localhost‘,        ‘PORT‘: ‘5432‘,    }}

Refer to the official documentation for database configuration items.

This will also set the project's time zone to utc+8 that is Beijing time, changing the time zone configuration to

TIME_ZONE = ‘Asia/Shanghai‘
1.3.2 Initializing a project

The project can now be initialized, and it should be explained that the original database Synchronization command has been marked as deprecation from Django 1.7, and that the syncdb command will be completely removed in 1.9, instead of its migrate command,

python3 manage.py migrate

It is necessary to note that because Django is pip3 installed in the real environment of Python 3, it is necessary to specify the running environment as Python 3 before executing the manage.py script, otherwise the Django will not be found and the use of virtualenv will be improved.

1.3.3 Start Service

Now you can start the Django built-in simple test with a Web server to test whether the whole project is working,

python3 manage.py runserver 0.0.0.0:8000

You need to specify the IP and port number, otherwise it can only be accessed locally.
Now open the user's browser, access to http://IP:8000 can see the debugging page, indicating that the program is running normally.
Terminating the service only requires Ctrl-C terminating the process.

2 Summary2.1 Process Summary

The whole process or because not familiar with a lot of detours, especially in my short board database this piece, to PostgreSQL also need to strengthen learning AH.

2.2 Things you can do

As a learning project to build the process, the article described here is barely enough, but also seems very humble, there is a lot of work to do, such as the use of virtualenv Management development environment, to avoid direct in real environment development. You can also use git for versioning, which is also necessary during the learning phase, and it helps to write down code that works correctly every time, preventing the program from running without a clue when a change occurs.
Of course, deploying Web apps can also use the uwsgi + nginx approach, but this is more complex and is not very meaningful for Django learning, and can be considered as another topic.

Remember the process of creating a Django app on Linux

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.