Non-web project using Django database connection postgreSQL

Non-web project using Django database connection postgreSQL

1.Django Installation
1.1 Installation command

pip install Django
detect the installation was successful:
#python
>>> Import Django
>>> django.VERSION
If the output Django version number, indicating a successful installation.

Low python version 1.2 appears abnormal solutions
1. Download the new version of the python:
sudo APT-GET install python3.5
2. Switching python version:
sudo RM / usr / bin / python
sudo -s /usr/bin/python3.5 LN / usr / bin / python
2.Django connection postgreSQL
2.1 to create a django project
1. use python development IDE pycharm: click: file-> new project, select Django column, enter a project name, and click create to create.
2. Create APP: APP may be included in each of the plurality of django project, that a large project subsystems, sub-module, functional components and so on, more independent from each other but also linked. All APP sharing project resources.
Enter the terminal command terminal below pycharm:
Python manage.py startapp testdb
This creates APP called testdb of, django automatically generated "testdb" folder.

2.2 postgreSQL database
1.postgreSQL database installation:
sudo APT-GET install PostgreSQL
installation is complete, the default will be:
(1) Create a Linux user named "postgres" of
(2) create a file named "postgres", with no default password as a database administrator database account
(3) creates a table named "postgres" of
2. Change the default administrator account password:
sudo -u Postgres psql
Postgres = # Postgres with the ALTER the User password '123456';
this way, the administrator "postgres "the password is" 123456. " Exit psql client command: \ q
3. Create a database postgreSQL
here I use Navicat postgreSQL create a database, see the installation and use of reference documents Navicat also can be created using the command line.
4. Configure database in Django project
(1) First registered app in settings.py file, it does not register, your database will not know which app to create the table.
App name to join already created under INSTALLED_APPS. (E.g. 'the testdb',)
(2) In the settings, parameters related to the configuration database, arranged at DATABASES:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db.postre', # database name
'USER': 'postgres', # owner, the general did not modify
'PASSWORD': '123456' # password, set their own
'HOST': '', # default is no write
'PORT': '5432',
}
5. create a .py file, whether the test can connect to the database.

import psycopg2
conn = psycopg2.connect(database="db.postre", user="postgres", 
password="123456", host="localhost", port="5432")
print "Opened database successfully"

If you print out
"Opened database successfully", it means that the database connection is successful.
Psycopg2 PostgreSQL database interface language of 2.3.Python installation:
the sudo APT-GET Update
the sudo the install APT-GET-Essential -Y Build
the sudo the install APT-GET -Y-dev python3.5
the sudo APT-GET the install the libpq -Y-dev
the sudo psycopg2 install PIP3
. 2.4 appears postgresql password can not be modified solution to the problem:
see below reference documentation.

3. programming and operation
(1) has been built in the app models.py edit files created here two fields, namely to save the user name and password:
class UserInfo (models.Model):
the User = models.CharField (MAX_LENGTH = 32)
pwd = models.CharField (MAX_LENGTH = 32)
(2) to create the next table in the database command in teminal pycharm. There are two commands, namely:

Python manage.py makemigrations
Python manage.py the migrate
(3) create a .py file in which to achieve operation of the database:

import sys
import os
#获取当前文件目录
pwd = os.path.dirname(os.path.realpath(__file__))
 #获取项目名的目录(因为我的当前文件是在项目名下的文件夹下的文件.所以是../)
sys.path.append(pwd+"../")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "项目名称l.settings")

import django
django.setup()

from testdb.models import UserInfo
def dbsave(user1,pwd1):
    saveuser = UserInfo(user=user1,pwd=pwd1)
    saveuser.save()
    print('添加成功')
def dbfind():
    list = UserInfo.objects.all()
    for var in list:
        print(var.user,var.pwd)
def dbfindname(name):
    find = UserInfo.objects.get(user=name)
    print(find.user,find.pwd)
def dbupdate(name,pwd1):
    update = UserInfo.objects.get(user = name)
    update.pwd = pwd1
    update.save()
    print('修改密码成功')
def dbdelete(name):
    find = UserInfo.objects.get(user=name)
    find.delete()
    print('删除成功')

if __name__ == "__main__":

    dbsave('liu','1265')
    dbfind()
    dbfindname('pop2')
    dbupdate('liu1','4523')
    dbdelete('pop2')

4. Reference documents:
Django installation and learning website: https://www.runoob.com/django/django-install.html
https://www.cnblogs.com/feixuelove1009/p/5823135.html
Python version installed appear in low solution:
https://www.cnblogs.com/wmr95/p/7637077.html
separate python script file that comes with using the django Model:
https://blog.csdn.net/bovenson/article/details/ 51,210,552

Ubuntu PostgreSQL Installation and Configuration: https://www.cnblogs.com/Siegel/p/6917213.html
Django postgreSQL connection configuration:
https://blog.csdn.net/wuxiaosi808/article/details/54375753
the Python language PostgreSQL database Interface psycopg2 installation:
https://cloud.tencent.com/developer/ask/64188

Navicat database management tools installation: https://www.jianshu.com/p/12501bdcfaf8
./start_navicat unresponsive to solve: https://www.cnblogs.com/hrhguanli/p/4548778.html
Navicat garbage problem: HTTPS : //blog.csdn.net/sinat_26546385/article/details/80381282

Postgresql password reset:
https://www.cnblogs.com/oxspirt/p/7217320.html?utm_source=itdadao&utm_medium=referral
https://www.cnblogs.com/terrysun/archive/2012/11/30/2796479. HTML
https://blog.csdn.net/tingyuanss/article/details/43763899
https://panyongzheng.iteye.com/blog/2238282

Guess you like

Origin blog.csdn.net/qq_32188669/article/details/90904140