Python Django 和 Ruby Rails的学习【原创】

这是我之前学习Django和Rails写的文章。总结整理出来。

Python Django

 

wget http://www.djangoproject.com/download/1.3.1/tarball/

tar xzvf Django-1.3.1.tar.gz

cd Django-1.3.1

python setup.py install

在目录下面创建

在/root/Django-1.3.1 下面创建 hhydjango.py

Code:

import django

print django.get_version()

django-admin.py startproject phperyang (建立一个 django 应用程序 phperyang)

产生文件:

__init__.py

manage.py

settings.py

urls.py

python manage.py runserver 就启动了(127.0.0.1:8000)

个人觉得settings.py和urls.py很重要

setting.py 使用sqlite [easy_install pysqlite命令]

DATABASES = {     'default': {         'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.         'NAME': '/database.db',# Or path to database file if using sqlite3.         'USER': '',                      # Not used with sqlite3.         'PASSWORD': '',                  # Not used with sqlite3.         'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.         'PORT': '',                      # Set to empty string for default. Not used with sqlite3.     } }

ADMIN_MEDIA_PREFIX = "yang_admin"

【会在生成的页面里面带有:

<link rel="stylesheet" type="text/css" href="/yang_admin/css/base.css" /> <link rel="stylesheet" type="text/css" href="/yang_admin/css/dashboard.css" /> <!--[if lte IE 7]><link rel="stylesheet" type="text/css" href="/yang_admin/css/ie.css" /><![endif]-->】

MIDDLEWARE_CLASSES = (     'django.middleware.common.CommonMiddleware',     'django.contrib.sessions.middleware.SessionMiddleware',     'django.middleware.csrf.CsrfViewMiddleware',     'django.contrib.auth.middleware.AuthenticationMiddleware',     'django.contrib.messages.middleware.MessageMiddleware',     #yang add     'django.middleware.csrf.CsrfViewMiddleware',     'django.middleware.csrf.CsrfResponseMiddleware', )

TEMPLATE_DIRS = (     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".     # Always use forward slashes, even on Windows.     # Don't forget to use absolute paths, not relative paths.     '/opt/www/Django-1.3.1/phperyang/templates' )

等等。。。省略。。。

urls.py

from django.conf.urls.defaults import patterns, include, url # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('',     # Uncomment the next line to enable the admin:     (r'^py/$', 'lists.views.index'),

    (r'^py/lists/$', 'lists.views.index'),

    (r'^py/lists/(?P<y_id>\d+)/$', 'lists.views.detail'),

.... )

...

在同目录下建立控制器/数据层文件夹 py

list.py

...

def yang(request, job_id):     yangphp = request.POST['msgssssss']     ver = django.get_version()     return render_to_response('jobs/msg.html', {'foo': yangphp, "ver" : ver} )

...

新建admin.py 在urls.py中修改路由规则

from jobs.models import Job from jobs.models import Location from django.contrib import admin admin.site.register(a) admin.site.register(b)

这样就自动建立了后台。

将Django 部署到正式的Apache 环境上。

安装mod_python

用httpd –M 来查看apache 是否加载mod_python

注意配置虚拟机:

<Location "/phperyang">   
      SetHandler python-program
      PythonHandler django.core.handlers.modpython
      SetEnv DJANGO_SETTINGS_MODULE phperyang.settings
      PythonPath "['/Django-1.3.1/phperyang', '/Django-1.3.1'] + sys.path"
      PythonDebug Off
</Location>

 

Ruby Rails

install ruby
install gem
注意版本
ruby -v gem -v
安装rails
gem install rails --include-dependencies 后面的标示依赖包也安装,也可以使用 gem install rails -y -v版本号
rails -v
进入到目录
rails new demo
rails server 启动rails的欢迎页面

ruby 1.9.2p180 (2011-02-18) [i386-mingw32]

C:\Documents and Settings\ThinkPad>e:

E:\>cd rubywork

E:\rubywork>rails new demo
      create
      create  README
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      ...

webbrowse : http://127.0.0.1:3000/ QeePHP的创建App就是仿照Ralis弄...

接着创建DEMO
rails generate controller welcome 创建一个控制器
在confg 中的 routes.rb 增加一行
get "welcome/say" => "welcome#say"

app/controllers
class WelcomeController < ApplicationController
    def say
        @y = " by phper.yang"
    @t = Time.now
        render :y => @y,:t => @t
    end
end
app/views/welcome

创建say.html.erb
<h1>Hello, World!Ruby Rails</h1>
<p>Hola! It's <%= @t %></p>
<%= @y %>

将Rails 部署到正式的Apache 环境上。

gem install passenger

passenger-install-apache2-module 

安装完成后会有提示,安装成功了,提示怎么修改Apache的虚拟机配置

   <VirtualHost *:80>

      ServerName www.yourhost.com

      DocumentRoot /somewhere/public    # <-- be sure to point to 'public'!

      <Directory /somewhere/public>

         AllowOverride all              # <-- relax Apache security settings

         Options -MultiViews            # <-- MultiViews must be turned off

      </Directory>

   </VirtualHost>

根据提示安装就好

这样apache 就支持rails了。

你可能感兴趣的:(Python Django 和 Ruby Rails的学习【原创】)