Skip to content

Instantly share code, notes, and snippets.

@hongru
Last active December 11, 2015 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongru/4662285 to your computer and use it in GitHub Desktop.
Save hongru/4662285 to your computer and use it in GitHub Desktop.
About Python&Django

Python27 install info for Windows

  • http://www.python.org/getit/releases/2.7.3/ 找到MSI installer 下载,建议用2.7的版本
  • 安装python后添加环境变量 PATH: ;C:\Python27
  • 安装python package包管理工具和常用package
  • 用pip安装各种需要的package,比如 pip install distribute ...
  • 安装 Django, pip install django
  • 安装 pil, pip install pil ,pil好像下载极其艰难 -_-#
  • django_admin.py startproject ***
  • manage.py runserver [8000]

Django Static Files Settings

  • settings.py
      import os.path
      fileRoot = os.path.dirname(__file__)
      MEDIA_ROOT = os.path.join(fileRoot, 'static')
      MEDIA_URL = '/site_media/'
    
  • urls.py
      from django.conf import settings
      if settings.DEBUG:
          urlpatterns += patterns("",
          (r'^site_media/(?P.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT,"show_indexes":True}),      
      )
    
  • templates/
      <link href="/site_media/**.css" rel="stylesheet" />
      <script src="/site_media/**.js"></script>
    

数据库接入

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

上述配置中 NAME 字段配置前提是本地的mysql数据库中有一个叫 python_hello 的table

  • 验证数据库连接成功
from django.db import connection
cursor = connection.cursor()
  • 新建app
python manage.py startapp ***
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment