Build Python + Django + MySQL in SAE (based on Windows) _ MySQL-mysql tutorial

Source: Internet
Author: User
Build Python + Django + MySQL (Windows-based) pythonDjango in SAE

In order to keep pace with the times, I started to study Python at work and was attracted by the "elegant" syntax of Python at first glance! Later I came into contact with Django. although I haven't studied it too deeply, I am very interested in this new concept WEB development and decided to use it instead of Java to develop small background programs, learn while using it!

Note: This article only describes the basic construction process and does not explain it because I do not understand Python!


I. Python Environment version used by SAE

Python2.7
Django1.27/1.4/1.5
Determine the local version and use other pre-installed modules.
(I first used Python and Django of a later version, and there were a lot of inexplicable errors .)

II. Register and configure SAE

Register and create an application. after the application is created, create a code version in "code management". SAE allows code of multiple versions to exist and switch between them.
SAE through SVN to manage Python source code, the bottom of the page is the source code SVN address, is "https://svn.sinaapp.com/<your app name>" such format, SVN account passwords are security email addresses and security passwords respectively.

SVN client http://tortoisesvn.net/downloads.html

The root directory is based on the code version. There should be only one folder labeled "1", with only two files: config. yaml and index. wsgi.

Create a Django project in the "1" folder and enter:

Startproject django-admin.py <你的app名字>

There will be two levels of paths named by your APP, and the subdirectories and manage. py will be copied to the level-1 Directory.
The root directories include config. yaml, index. wsgi, manage. py, and folder.
The folders include _ init _. py, setting. py, urls. py, and wsgi. py.

3. modify config. yaml and index. wsgi:

Config. yaml:
Name:
 <你的app名字>
  
Version: 1 libraries:-name: "django" version: "1.5" # I am using Django1.5
 

Index. wsgi:
Import OS import django. core. handlers. wsgiimport saeos. environ ['Django _ SETTINGS_MODULE '] ='
 <你的app名字>
  
. Settings 'application = sae. create_wsgi_app (django. core. handlers. wsgi. WSGIHandler ())
 

4. debug Django

We recommend that you use pycharm for IDE development, which is very convenient.

: Http://www.jetbrains.com/pycharm/


Install SAE in the simulation environment of the local machine.

: Https://github.com/sinacloud/sae-python-dev-guide.git

Run python setup. py install on dev_server.

If "No module name setuptools" is displayed, it indicates that setuptools is missing.

: Https://bootstrap.pypa.io/ez_setup.py

Save it and run the following command to automatically install it.


Modify the database link in setting. py and add and modify the following code:
Import OS. pathfrom OS import environdebug = not environ. get ("APP_NAME", "") if debug: # LOCAL when the code runs locally, link to the LOCAL database, configure MYSQL_DB = 'app _ saepy 'MYSQL_USER = 'root' MYSQL_PASS = 'admin' MYSQL_HOST_M = '2017. 0.0.1 'mysql_host_s = '127. 0.0.1 'mysql_port = '000000' else: # When the code runs on the cloud platform, the SAEs are connected to the apsaradb. the parameters used for the connection are all in SAEs. import sae in const. const MYSQL_DB = sae. const. MYSQL_DB MYSQL_USER = sae. const. MYSQL_USER MYSQL_PASS = sae. const. MYSQL_PASS MYSQL_HOST_M = sae. const. MYSQL_HOST MYSQL_HOST_S = sae. const. MYSQL_HOST_S MYSQL_PORT = sae. const. MYSQL_PORTDATABASES = {'default': {'engine ': 'Django. db. backends. mysql ', 'name': MYSQL_DB, 'user': MYSQL_USER, 'password': MYSQL_PASS, 'host': MYSQL_HOST_M, 'port': MYSQL_PORT ,}}

To test the function of opening the built-in admin of Django, modify urls. py and setting. py.

Start the service locally or through PyCharm
Manage. py runserver 8000
You should be able to access the Django program through localhost: 8000.

Note that SAE MySQL does not allow access by tools such as Navicat for security reasons. Syncdb commands can also be used to synchronize databases of SAE. the solution is to use the syncdb command locally, convert the local database to an SQL file, and then execute the command on MySQL of SAE, this is much more convenient. (Before using MySQL of SAE, remember to initialize the database in "service management .)

Upload a locally configured Django project through SVN. it is not surprising that .sinaapp.com/admin/should have been handled, but it is the management interface and there is no cssstate. This is because Django cannot find the admin static file. There are many solutions, which are described in the official documentation. it is relatively simple to copy the static file used by admin to the "1 ( <在django安装路径> The admin folder under/django/contrib/admin/static.

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.