日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python实现第一个web_我的第一个Python Web应用

發布時間:2023/12/18 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python实现第一个web_我的第一个Python Web应用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實現的是通信錄的Web應用,在Windows xp環境下開發。

1.從官方網站下載Python安裝文件,安裝后配置環境變量(系統變量path)。

C:\Program Files\Python25;

C:\Program Files\Python25\Scripts;

2.下載Django,解壓。打開命令行,進入剛解壓的目錄,執行python setup.py install,然后把Django中bin目錄的路徑添加到環境變量path里面。

3.現在打開命令提示符,進入到想要創建應用的目錄后鍵入django-admin.py startproject mysite命令,調用Django的控制臺命令新建一個名為mysite的工程,與此同時Django還在新創建的mysite文件夾下生成以下四個分工不同的文件: __init__.py? ? manage.py??? settings.py ?? urls.py

4.在命令提示符下進入工程目錄,鍵入命令manage.py runserver,就可以啟動Web服務器來測試新建立的工程。瀏覽器中輸入http://127.0.0.1:8000/ 查看效果(Ctrl+C可停止服務器)。

5.在工程建立好之后,接下來就可以編寫Django的應用模塊。鍵入命令python manage.py startapp contacts ,命令會在當前工程下生成一個名為article的模塊,目錄下除了標識Python模塊的__init__.py文件,還有額外的兩個文件models.py和views.py。

6.在傳統的Web的開發中,很大的一部分工作量被消耗在數據庫中創建需要的數據表和設置表字段上,而Django為此提供了輕量級的解決方案。借助Django內部的對象關系映射機制,可以用Python語言實現對數據庫表中的實體進行操作,實體模型的描述需要在文件models.py中配置。

from django.db import models

from django.contrib import admin

class Area(models.Model):

areaname = models.CharField(max_length=20)

def __str__(self):

return self.areaname.encode('utf-8')

class Meta:

ordering = ['areaname']

class Admin:

pass

class Dept(models.Model):

deptname = models.CharField(max_length=20)

def __str__(self):

return self.deptname.encode('utf-8')

class Meta:

ordering = ['deptname']

class Admin:

pass

class Employee(models.Model):

employee_area = models.ForeignKey(Area)

employee_dept = models.ForeignKey(Dept)

name = models.CharField(max_length=20)

english_name = models.CharField(max_length=40)

tel = models.CharField(max_length=20)

cell_phone = models.CharField(max_length=20)

email = models.CharField(max_length=40)

def __str__(self):

return self.name.encode('utf-8')

class Meta:

ordering = ['employee_area', 'employee_dept', 'name']

class Admin:

pass

admin.site.register([Area,Dept,Employee])

return self.name.encode('utf-8') #開始我寫的return self.name,結果不能插入中文

from django.contrib import admin #我沒有導入這個,雖然能進入Django管理,但數據庫表的管理卻沒有

7.修改setting.py

# Django settings for firstproject project.

import os

DEBUG = True

TEMPLATE_DEBUG = DEBUG

SITE_PATH = os.path.dirname(os.path.abspath(__file__)) #System Path

ADMINS = (

# ('Your Name', 'your_email@domain.com'),

)

MANAGERS = ADMINS

DATABASES = {

'default': {

'ENGINE': 'sqlite3',

# Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.

'NAME': 'F:/firstproject/contacts.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.

}

}

# Local time zone for this installation. Choices can be found here:

# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name

# although not all choices may be available on all operating systems.

# On Unix systems, a value of None will cause Django to use the same

# timezone as the operating system.

# If running in a Windows environment this must be set to the same as your

# system time zone.

TIME_ZONE = 'America/Chicago'

# Language code for this installation. All choices can be found here:

# http://www.i18nguy.com/unicode/language-identifiers.html

LANGUAGE_CODE = 'zh-CN'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not

# to load the internationalization machinery.

USE_I18N = True

# If you set this to False, Django will not format dates, numbers and

# calendars according to the current locale

USE_L10N = True

# Absolute filesystem path to the directory that will hold user-uploaded files.

# Example: "/home/media/media.lawrence.com/"

MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a

# trailing slash if there is a path component (optional in other cases).

# Examples: "http://media.lawrence.com", "http://example.com/media/"

MEDIA_URL = ''

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a

# trailing slash.

# Examples: "http://foo.com/media/", "/media/".

ADMIN_MEDIA_PREFIX = '/media/'

# Make this unique, and don't share it with anybody.

SECRET_KEY = '95*^$#d8s3z79x@6l$10)g=#_6*u7(j4bsx+m7djf$!+qw+8w0'

# List of callables that know how to import templates from various sources.

TEMPLATE_LOADERS = (

'django.template.loaders.filesystem.Loader',

'django.template.loaders.app_directories.Loader',

# 'django.template.loaders.eggs.Loader',

)

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',

)

ROOT_URLCONF = 'firstproject.urls'

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.

)

INSTALLED_APPS = (

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.sites',

'django.contrib.messages',

# Uncomment the next line to enable the admin:

'django.contrib.admin',

'firstproject.contacts',

# Uncomment the next line to enable admin documentation:

# 'django.contrib.admindocs',

)

#My note display Chinese

FILE_CHARSET='gb18030'

DEFAULT_CHARSET='utf-8'

8.修改urls.py

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:

from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',

# Example:

# (r'^firstproject/', include('firstproject.foo.urls')),

# Uncomment the admin/doc line below to enable admin documentation:

# (r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:

(r'^admin/', include(admin.site.urls)),

)

9.在News工程的命令提示符下執行manage.py

syncdb指令。Django會根據模型的定義自動完成ORM的數據庫映射工作,屏蔽了底層數據庫細節和SQL查詢的編寫。輸入yes,創建賬戶,電子郵件,密碼。

10.次使用命令manage.py runserver來啟動Django自帶的Web服務器后,在瀏覽器中訪問地址http://127.0.0.1:8000/admin/

,使用剛才創建的superuser用戶的賬號和密碼登陸

總結

以上是生活随笔為你收集整理的python实现第一个web_我的第一个Python Web应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。