Sae-based Python + Django getting started

Source: Internet
Author: User
Tags import database
Sae-based Python + Django getting started

Sina Sae has started to support Python and supports the Django framework.

Click here for details: http://appstack.sinaapp.com/

Douban group: http://www.douban.com/group/373262/

Next, go to the topic.

First, I use a combination of Django and MySQL, so this article does not involve other frameworks.

Second, I am a cainiao. If you have any suggestions for improvement, please don't hesitate to give me some advice. Thank you very much.

 

 

Step 1 Application

First, you must have an sae account and use Sina Weibo to register. Register, log in, and then go straight to here http://appstack.sinaapp.com/queue to queue for Sae
Python. If you already have an invitation code and can add a Python application, ignore this section. Queue. How can I check whether the service is activated ?, ......

Now you have activated SAE python. You can create an application and a Python application.

 

Step 2 create

1. Come to your SAE homepage http://sae.sina.com.cn /? M = MyApp, create a new application, and select python as the development language:

2. Return to the Application List and click your application name to go to the Application Management page:

3. Go to the left navigation bar and choose Service Management> mysql. The page appears.

,

Initialize MySQL.

4. Similarly, you can enable all the services listed in service management.

5. We can start to create the first version of our first application:

(Still on the Left bar)-Application Management-code management, click "Create a version here", Version Number 1, create.

After successful creation, you can see similar information:

 

The next step is to deploy your code using SVN.

 

Step 3 Deployment

1. Now suppose you have installed SVN properly. If you don't know about SVN, click here: http://sae.sina.com.cn /? M = devcenter & content_id = 215 & catid = 212

Note! The user name used for SVN check-out is your Weibo account, but the password is a secure password!

2. Check out the code folder to your local computer. The folder named after the version number is displayed. Here it should be 1.

Go to the folder 1 and find the config. yaml and index. wsgi files. Open index. wsgi in a text editor and delete all the contents. Use the following code instead.

Import OS

Import Django. Core. Handlers. wsgi

Import SAE

OS. Environ ['django _ settings_module '] = 'mysite. setting' # Replace mysite with your application name.

Application = Sae. create_wsgi_app (Django. Core. Handlers. wsgi. wsgihandler ())

Note! Do not forget to replace the mysite in the OS. Environ ['django _ settings_module '] = 'mysite. setting' line with the name of the application you just created on Sae!

In addition, all the mysites shown below should be replaced with your application name on Sae!

3. If you want to use the admin module provided by Django, copy the admin media directory from the Django installation directory to folder 1.

4. If you have already created a Django project, directly put the folder (the project folder name must be the same as the application name created on SAE) under folder 1.

If you have not created a Django project, go to folder 1 and run the python django-admin.py startpreject mysite command (replace mysite with the application name created on SAE, which will not be prompted below)

In this step, make sure that there is your Django project under folder 1, that is, the mysite folder. That is, make sure that the following information is displayed in folder 1:

Index. wsgi config. yaml mysite (project folder)

5. Go to the mysite directory and modify settings. py:

A. Add the following code to the header.

Import OS. Path

From OS import environ

DEBUG = Not Environ. Get ("app_name ","")

If debug:

# Local debugging is used to facilitate Database Export. Enter the following parameters according to the local MySQL database:

Mysql_db = 'mysite'

Mysql_user = 'root'

Mysql_pass =''

Mysql_host_m = '2017. 0.0.1'

Mysql_host_s = '2017. 0.0.1'

Mysql_port = '123'

Else:

# SAE

Import Sae. 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_port

B. Modify the databases field:

Databases = {

'Default ':{

'Engine': 'django. DB. backends. mysql ',

'Name': mysql_db,

'User': mysql_user,

'Password': mysql_pass,

'Host': mysql_host_m,

'Port': mysql_port,

}

}

C. If you have set your own templates directory, add the field to template_dirs.

'/Usr/local/SAE/Python/lib/python2.6/Site-packages/Django/contrib/admin/templates/admin', as follows:

Template_dirs = (

'/Usr/local/SAE/Python/lib/python2.6/Site-packages/Django/contrib/admin/templates/admin ',

OS. path. join (OS. path. dirname (_ file _), 'templates '). replace ('\', '/'), # You have set the tempalets directory under the mysite directory to the template directory.

)

 

Step 4 Import Database

1. the mysql-Python module is required for local MySQL operations,

The win system can download the installation file here with ease: (for python2.6http: // www.codegood.com/archives/4,for)
Python2.7http: // www.codegood.com/archives/129 ),

Linux is here: http://pypi.python.org/pypi/MySQL-python#downloads

2. If you have installed the MySQL Service locally or you can connect to the MySQL database, you can directly view the next one.

If you do not have a MySQL database locally, you can install wampserver: http://www.onlinedown.net/soft/82112.htm

After installation, do not forget to restart the machine and double-click

Shortcut: Click the small icon on the tray to start all services. The startup is successful and the icon turns green.

Click the tray icon again and click phpMyAdmin to open the Database Manager from the browser. Click "Database" at the top of the page"

Create Database

After the creation is complete, the left column shows that mysite has been added.

Click mysite to open the management page of the local mysite database.

3. Modify settings. py in the mysite directory

DEBUG = Not Environ. Get ("app_name ","")

If debug:

# Local debugging is used to facilitate Database Export. Enter the following parameters according to the local MySQL database:

Mysql_db = 'mysite'

Mysql_user = 'root'

Mysql_pass =''

Mysql_host_m = '2017. 0.0.1'

Mysql_host_s = '2017. 0.0.1'

Mysql_port = '123'

If you follow steps 2 and in, this section does not need to be modified.

If you are using an existing database, you can easily see how to change it.

4. Go to the mysite directory and run Python manage. py syncdb. After that, export the local database:

Go to the Management page of the local mysite database, click Export-execute at the top, and output the SQL file.

Import the exported database to the remote MySQL database of the application created in SAE:

Go to the Application Management page of SAE> Service Management> mysql> Manage MySQL to go To the MySQL Management page of SAE. Click Import at the top-> select a file (the SQL file output just now)-> Execute

 

Step 5 upload

Submit all the files in directory 1 to sae through SVN, and mysite can work now!

I guess you won't forget to change DEBUG = true in settings. py to DEBUG = false.

Related Article

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.