Django+Python+GoogleAppEngine+HTML/XHTML+CSS = WEB APP

本文介绍如何将基于Django的数据存储应用部署到Google App Engine上。主要内容包括创建项目目录、设置main.py和app.yaml文件、配置settings.py、定义URL模式、建立数据模型、编辑视图文件、完成模板、本地测试及最终上传应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(自己根据网上的各种资料总结了一个简洁有效的版本,希望能对这方面的新手有帮助)

In this tutorial I will show you how to get a simple datastore-backed Django application up and running on the  Google App Engine. I will assume that you are somewhat familiar with Django and Python.

1, Create a directory where you would like to keep all your Appengine and Django files. Inside of this directory, run the command:
django-admin.py startproject appproject

 

2, Create a file called main.py at the same level as the folder appproject, and put this code:

 

import os,sys
os
.environ['DJANGO_SETTINGS_MODULE'] = 'appproject.settings'#your own app name


# Google App Engine imports.
from google.appengine.ext.webapp import util

# Force Django to reload its settings.
from django.conf import settings
settings
._target = None

import django.core.handlers.wsgi
import django.core.signals
import django.db
import django.dispatch.dispatcher

# Log errors.
#django.dispatch.dispatcher.connect(
#   log_exception, django.core.signals.got_request_exception)

# Unregister the rollback event handler.
django
.dispatch.dispatcher.disconnect(
    django
.db._rollback_on_exception,
    django
.core.signals.got_request_exception)

def main():
  
# Create a Django application for WSGI.
  application 
= django.core.handlers.wsgi.WSGIHandler()

  
# Run the WSGI CGI handler with that application.
  util
.run_wsgi_app(application)

if __name__ == '__main__':
  main
()


3,Create another file called  app.yaml , and put in this code:
application
: appproject               #use your own app id here
version
: 1
runtime
: python
api_version
: 1

handlers
:
- url: /.*
  script
: main.py


4, Navigate to folder appproject, and run the command:
python manage.py startapp  myapp

5, Edit the  settings.py so  MIDDLEWARE_CLASSES and  INSTALLED_APPS look like this.:  MIDDLEWARE_CLASSES = (
    
'django.middleware.common.CommonMiddleware',
)

INSTALLED_APPS 
= (
    
'appproject.myapp'
)

Also we need to tell Django where to find HTML files which can serve as the basis of displaying the pages. Change TEMPLATE_DIRS to look like this:


import os
ROOT_PATH 
= os.path.dirname(__file__)
TEMPLATE_DIRS 
=(
    ROOT_PATH 
+'/templates',      #DON'T forget to mkdir template
)


6, Django uses regular expressions, to tell which function must handle a given URL. Edit the file  urls.py to this:

from django.conf.urls.defaults import *

urlpatterns 
= patterns('',
    
(r'^$', 'appproject.myapp.views.myviewfunction'),#to be changed here

    )


7, You need to define your datamodel in  models.py file.  (an example here)

from
 google.appengine.ext import db

class Example(db.Model):
    title 
= db.StringProperty()
    created_on 
= db.DateTimeProperty(auto_now_add = 1)
    created_by 
= db.UserProperty()

    
def __str__(self):
        
return '%s' %self.title

    
def get_absolute_url(self):
        
return '/youURLS/%s/' % self.key()

8, Edit the views.py file to mapped to the four URL patterns.

from django.http import HttpResponse, HttpResponseRedirect
from appproject.myapp import models
import maybe_something_yours_here
from django.shortcuts import render_to_response

def render(template, payload):
    payload
['recents'] = models.Poll.all().order('-created_on').fetch(5)
    
return render_to_response(template, payload)

def index(request):
    polls 
= models.Poll.all().order('-created_on').fetch(20)
    payload 
= dict(polls = polls)
    
return render('index.html', payload)


9, now we need complete the templates.

10, we can test it now:
dev_appserver.py yourprojectname/
THEN,Point your browser towards  http://127.0.0.1:8080/

11, GREAT! the next, Upload your application to the Google App Engine:

appcfg.py update yourprojectname/


That's all. Enjoy it!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值