Python Django 和 Ruby Rails的學習【原創】

這是我以前學習Django和Rails寫的文章。總結整理出來。php

Python Django css

 

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

tar xzvf Django-1.3.1.tar.gzjava

cd Django-1.3.1python

python setup.py installmysql

在目錄下面建立git

在/root/Django-1.3.1 下面建立 hhydjango.pyweb

Code:sql

import djangoapache

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

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)

這樣就自動創建了後臺。

[easy_install pysqlite命令]

將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了。
相關文章
相關標籤/搜索