简体   繁体   中英

Django database in Python3

I'm writing a web application using Python 3.2 & Django 1.6 . I need a secure database but Django doesn't support mysql on python 3 so the only choice for me is sqlite . I there anyway to secure this kind of database using .htaacess or I can try other kind of databases?

Your sensitive data : django scripts, databases, logs, ... should always be outside the root web folder so there is no possible access from the web. Using an .htaccess is not the good way of approaching the problem security-wise.

If you REALLY need this, then make a directory rule to disallow the folder containing the sqlite database in your apache sites configuration or add a .htaccess file containing the following into the folder where you have your sqlite database:

deny from all

That should do it. But again, the above solution is not a good practice , you should configure your webserver so that the web root is in a public/ subfolder:

[project root]
    [project name]
        [media]
        [static]
        [templates]
        [public]      <= your webserver should point to this directory as root
                         (see below); used in production with collecstatic method
        [data]
            [db]
                mydata.sqlite      <= you could put your sqlite database here, 
                                      available to your django app and unavailable
                                      to web users.
        settings.py
        urls.py
        views.py
    manage.py

Apache should be configured so as that your domain, let's say example.com has a DocumentRoot that points to [...]/project root/project name/public/ so that all other folders are unavailable from the outside.

You can use PyMySQL. Because PyMySQL can import as MySQLdb.

Like this:

pip install PyMySQL

add followed code to site's __init__.py

import pymysql

pymysql.install_as_MySQLdb()

Django support 4 tipe of Database engine:

For the most part , all the engines listed here work just as well with the core Django framework ( the notable exception should be raised to support optional GIS Django , which is much more powerful than with other PostgreSQL database). If you're not tied to a particular legacy system and have the freedom to choose any database backend , we recommend PostgreSQL , which is based on a balance between cost, features , speed and stability.

The setting of the database is a process that takes place in two stages:

First, you must install and configure the database server itself. This process is beyond the scope of this book, but each of the four databases listed here have extensive documentation on its website . ( If you are on a shared hosting , it is very likely that your provider has already set all this for you ) . Secondly, you need to install the Python library for your particular database backend. This is done through third-party code that allows Python to interact with the database.

If you're just playing with Django and do not want to install a database server, consider using SQLite. SQLite is unique in the list of supported databases that does not require either of the two previous steps. It merely read and write data to a single file on the filesystem and Python versions from 2.5 support it NATIVELY.

Yes I confirm SQL LITE that is supported NATIVELY from 2.5 version PHYTON.

Currently, the support of mysql in python3 and django is not that good. We can use the Mysql connector/Python from the mysql web site: http://dev.mysql.com/downloads/connector/python/

We'd better download the platform independent version and install them manually:

tar xvf mysql-connector-python-1.1.6.tar.gz

cd mysql-connector-python-1.1.6 

sudo python3 setup.py install

And in order to operate in mysql database, we should change the django database setting part this way:

DATABASES = {
'default': {
    'NAME': 'user_data',
    'ENGINE': 'mysql.connector.django',
    'USER': 'mysql_user',
    'PASSWORD': 'password',
    'OPTIONS': {
      'autocommit': True,
    },
  }
}

Attention: the ENGINE part in configuration should be 'mysql.connector.django', it is different from the original string 'django.db.backends.mysql'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM