English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
我的操作系统为centos6.5
1 首先选择django要使用什么数据库。django1.10默认数据库为sqlite3,本人想使用mysql数据库,但为了测试方便顺便要安装一下sqlite开发包。
yum install mysql mysql-devel #为了测试方便,我们需要安装sqlite-devel包 yum install sqlite-devel
2 接下来需要安装Python了,因为Python3已经成为主流,所以接下来我们要安装Python3,到官网去下载Python3的新版本。本人下载的版本为python3.5.2
wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
3 解压并安装
# 解压tar包 tar xf Python-3.5.2.tgz # 进入解压后的包 cd Python-3.5.2 # Configure installation information, my installation path is /usr/install/python3/ ./configure --prefix=/usr/install/python3/ # Compile and install make && make install
4 Configure the PATH environment variable
# Create a new file python3.sh under /etc/profile.d/ vim /etc/profile.d/python3.sh # Add the following line export PATH=$PATH:/usr/install/python3/bin/ # Then execute export PATH=$PATH:/usr/install/python3/bin/
5 By default, after installing Python3.5.2, pip is already installed, but I want to install a newer version of pip
# Download the pip installation program wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py # Install pip python3 get-pip.py
6 Install django
pip install Django
7 Install mysqlclient, mysqlclient is a connector for Python3 and mysql.
pip install mysqlclient
So far, Python and django installation is complete!
How to configure mysql as the default database for django?
1 Create a new project
# Create a project named mysite django-admin startproject mysite
2 Enter the project and modify the settings configuration file
# Enter the project cd mysite # Modify the settings configuration file vim mysite/settings.py # Find the DATABASES attribute DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # Set mysql as the default database for django 'NAME':'mysite', # Database configuration name: 'mysite' 'USER':'root', # Database user: 'root' 'PASSWORD':'123456', # Password: '123456' 'HOST':'127.0.0.1', # Configura l'indirizzo dell'host del servizio database, se è vuoto, viene utilizzato di default localhost 'PORT':'3306', # Configura la porta } }
3 Django non crea automaticamente il database per noi, dobbiamo crearlo manualmente.
# Avvia il servizio database service mysqld start # Accedi al database e entra nell'interfaccia a riga di comando del database mysql # Crea un database chiamato mysite. Nella configurazione del file settings, abbiamo definito il nome del database come mysite mysql>CREATE DATABASE mysite CHARACTER SET=utf8; # Comando di uscita dalla interfaccia a riga di comando del database mysql> quit
4 Crea un'app chiamata polls nel progetto mysite
[root@bogon mysite]# python3 manage.py startapp polls
5 Modifica il file polls/models.py
# vim polls/models.py # Modifica come segue: from django.db import models # Create your models here. class student(models.Model): name=models.CharField(max_length=24) school=models.CharField(choices=(('sc01','第一中学'),('sc02','第二中学'),('sc03','第三中学')),max_length=32) sfid=models.IntegerField(primary_key=True,unique=True,) phone=models.IntegerField(blank=True,null=True) emial=models.EmailField(null=True,blank=True) def __str__(self): return self.name
Se vuoi capire i metodi come models.CharField(), puoi consultare il mio articolo: Model Field in django.
6 Configurare l'attributo INSTALLED_APPS nel file settings
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'polls.apps.PollsConfig', # Aggiungere questa riga ]
7 (Questa fase può essere omessa) Notificare a django che il file models di polls è stato modificato.
python3 manage.py makemigrations poll
8 (Questa fase può essere omessa) Se si desidera sapere come le modifiche apportate a polls/models.py saranno applicate alla base dati, si può utilizzare il comando seguente:
python3 manage.py sqlmigrate polls 0001
9 (Questa fase può essere omessa) Per applicare le modifiche apportate ai file models alla base dati, eseguire il seguente comando:
python manage.py migrate
10 (Questa fase può essere omessa) Se si desidera eseguire l'aggiunta, eliminazione, ricerca e modifica dei modelli personalizzati nell'interfaccia di admin, è necessario modificare il file admin.py nella directory dell'app.
from .models import student # Iscrizione del modello student admin.site.register(student)
Questo è tutto il contenuto del metodo di installazione di Python3 e django sotto Linux e la configurazione di mysql come server predefinito di django fornito dall'autore. Spero che riceviate molta supporto e incoraggiamento dal tutorial Shouting~