问题描述
我想稍微远离 PHP 并学习 Python.为了使用 Python 进行 Web 开发,我需要一个框架来帮助处理模板和其他事情.
我有一台非生产服务器,用于测试所有 Web 开发内容.它是一个运行 MariaDB 的 Debian 7.1 LAMP 堆栈,而不是常见的 MySQL 服务器包.
昨天我安装了 Django 并创建了我的第一个名为 firstweb 的项目.我还没有更改任何设置.
这是我的第一个大困惑.在教程中,我跟随那个人安装了 Django,开始了他的第一个项目,重新启动了 Apache,从那时起 Django 就开始工作了.他打开浏览器,打开 Django 默认页面,没有任何问题.
然而,我必须进入我的 firstweb 文件夹并运行
python manage.py runserver myip:port
它有效.没问题.但我想知道它是否应该像这样工作,这是否会导致问题?
我的第二个问题是我想设置它以便它使用我的 MySQL 数据库.我进入/firstweb/firstweb 下的 settings.py,我看到了 ENGINE 和 NAME,但我不知道该放什么.
然后在 USER、PASSWORD 和 HOST 区域中,这是我的数据库及其凭据吗?如果我使用 localhost 可以将 localhost 放在 HOST 区域吗?
MySQL 支持 添加起来很简单.在您的 DATABASES
字典中,您将有这样的条目:
DATABASES = {'默认': {'引擎':'django.db.backends.mysql','NAME': 'DB_NAME','USER': 'DB_USER','密码': 'DB_PASSWORD','HOST': 'localhost', # 或者你的数据库所在的 IP 地址'端口':'3306',}}
您还可以选择使用 MySQL option文件,从 Django 1.7 开始.您可以通过像这样设置 DATABASES
数组来完成此操作:
DATABASES = {'默认': {'引擎':'django.db.backends.mysql','选项': {'read_default_file': '/path/to/my.cnf',},}}
您还需要使用与上面类似的设置创建 /path/to/my.cnf
文件
[客户]数据库 = DB_NAME主机 = 本地主机用户 = DB_USER密码 = DB_PASSWORD默认字符集 = utf8
使用 Django 1.7 中的这种新连接方法,了解建立连接的顺序很重要:
1.选项.2. 名称、用户、密码、主机、端口3. MySQL 选项文件.
<块引用>
换句话说,如果您在 OPTIONS 中设置数据库的名称,这将优先于 NAME,后者将覆盖 MySQL 选项文件中的任何内容.
<小时>
如果你只是在本地机器上测试你的应用程序,你可以使用
python manage.py runserver
添加 ip:port
参数允许您自己以外的机器访问您的开发应用程序.准备好部署应用程序后,我建议您查看关于部署 Django<的章节/a> 在 djangobook
Mysql 默认字符集通常不是 utf-8,因此请确保使用此 sql 创建数据库:
CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_bin
如果您使用 Oracle 的 MySQL 连接器 你的 ENGINE
行应该是这样的:
'ENGINE': 'mysql.connector.django',
<小时>
请注意,您首先需要在您的操作系统上安装 mysql.
brew install mysql (MacOS)
此外,mysql 客户端包已针对 python 3 进行了更改(MySQL-Client
仅适用于 python 2)
pip3 安装mysqlclient
I want to move away from PHP a little and learn Python. In order to do web development with Python I'll need a framework to help with templating and other things.
I have a non-production server that I use to test all of web development stuff on. It is a Debian 7.1 LAMP stack that runs MariaDB instead of the common MySQL-server package.
Yesterday I installed Django and created my first project called firstweb. I have not changed any settings yet.
Here is my first big piece of confusion. In the tutorial I followed the guy installed Django, started his first project, restarted Apache, and Django just worked from then on. He went to his browser and went to the Django default page with no problems.
Me however, I have to cd into my firstweb folder and run
python manage.py runserver myip:port
And it works. No problem. But I'm wondering if it is supposed to work like this, and if this will cause problems down the line?
My second question is that I want to set it up so it uses my MySQL database. I go into my settings.py under /firstweb/firstweb and I see ENGINE and NAME but I'm not sure what to put here.
And then in the USER, PASSWORD, and HOST areas is this my database and its credentials? If I am using localhost can I just put localhost in the HOST area?
MySQL support is simple to add. In your DATABASES
dictionary, you will have an entry like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
You also have the option of utilizing MySQL option files, as of Django 1.7. You can accomplish this by setting your DATABASES
array like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/path/to/my.cnf',
},
}
}
You also need to create the /path/to/my.cnf
file with similar settings from above
[client]
database = DB_NAME
host = localhost
user = DB_USER
password = DB_PASSWORD
default-character-set = utf8
With this new method of connecting in Django 1.7, it is important to know the order connections are established:
1. OPTIONS.
2. NAME, USER, PASSWORD, HOST, PORT
3. MySQL option files.
In other words, if you set the name of the database in OPTIONS, this will take precedence over NAME, which would override anything in a MySQL option file.
If you are just testing your application on your local machine, you can use
python manage.py runserver
Adding the ip:port
argument allows machines other than your own to access your development application. Once you are ready to deploy your application, I recommend taking a look at the chapter on Deploying Django on the djangobook
Mysql default character set is often not utf-8, therefore make sure to create your database using this sql:
CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_bin
If you are using Oracle's MySQL connector your ENGINE
line should look like this:
'ENGINE': 'mysql.connector.django',
Note that you will first need to install mysql on your OS.
brew install mysql (MacOS)
Also, the mysql client package has changed for python 3 (MySQL-Client
works only for python 2)
pip3 install mysqlclient
这篇关于设置 Django 以使用 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!