Python脚本之django---mysql

2017-09-13 10:27:15来源:http://qqran.blog.51cto.com/10014850/1964619作者:人点击

分享

#######

安装python-MySQLdb

##############

[root@LVS1 myweb]# pwd

/tmp/python/Django-1.5.1/django/bin/myweb/myweb

[root@LVS1 myweb]# vi settings.py

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.

'NAME': 'python', # Or path to database file if using sqlite3.

# The following settings are not used with sqlite3:

'USER': 'root',

'PASSWORD': '123456',

'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TC

P.

'PORT': '', # Set to empty string for default.

}

}

INSTALLED_APPS = (

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.sites',

'django.contrib.messages',

'django.contrib.staticfiles',

# Uncomment the next line to enable the admin:

# 'django.contrib.admin',

# Uncomment the next line to enable admin documentation:

# 'django.contrib.admindocs',

'pyweb'

[root@LVS1 myweb]# pwd

/tmp/python/Django-1.5.1/django/bin/myweb

[root@LVS1 myweb]# ./manage.py startapp pyweb

[root@LVS1 pyweb]# pwd

/tmp/python/Django-1.5.1/django/bin/myweb/pyweb

[root@LVS1 pyweb]# vi models.py

from django.db import models

# Create your models here.

from django.db import models

class Publisher(models.Model):

name = models.CharField(max_length=30)

address = models.CharField(max_length=50)

city = models.CharField(max_length=60)

state_province = models.CharField(max_length=30)

country = models.CharField(max_length=50)

website = models.URLField()

def __unicode__(self):

return self.name

class Author(models.Model):

first_name = models.CharField(max_length=30)

last_name = models.CharField(max_length=40)

email = models.EmailField()

def __unicode__(self):

return self.name

class Book(models.Model):

title = models.CharField(max_length=100)

authors = models.ManyToManyField(Author)

publisher = models.ForeignKey(Publisher)

publication_date = models.DateField()

def __unicode__(self):

return self.name

[root@LVS1 myweb]# pwd

/tmp/python/Django-1.5.1/django/bin/myweb

[root@LVS1 myweb]# python manage.py validate

0 errors found

[root@LVS1 myweb]# python manage.py sqlall pyweb

[root@LVS1 myweb]# python manage.py syncdb

[root@LVS1 myweb]# ./manage.py shell

In [3]: p1 = Publisher(name='shanghai', address='24242 chuansha road',city='ShangHai', state_province='CN', country='China',website='http://www.xxk.com/')

In [4]: p1.save()

In [5]: p1.name='hefei'

In [6]: p1.save()

---------------------------------------------------web站点

[root@LVS1 myweb]# pwd

/tmp/python/Django-1.5.1/django/bin/myweb/myweb

[root@LVS1 myweb]# vi settings.py

INSTALLED_APPS = (

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.sites',

'django.contrib.messages',

'django.contrib.staticfiles',

# Uncomment the next line to enable the admin:

'django.contrib.admin',

# Uncomment the next line to enable admin documentation:

# 'django.contrib.admindocs',

'pyweb'

)

[root@LVS1 myweb]# vi urls.py

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:

from django.contrib import admin

admin.autodiscover()

from myweb.view import hello,current_time,cpu,hours_ahead

from myweb.view2 import disk

urlpatterns = patterns('',

# Examples:

# url(r'^$', 'myweb.views.home', name='home'),

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

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

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

# Uncomment the next line to enable the admin:

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

(r'^hello/$',hello),

(r'^time/$',current_time),

(r'^cpu/$',cpu),

(r'^time/plus/(/d{1,2})/$',hours_ahead),

(r'^disk/$',disk),

)

[root@LVS1 pyweb]# vi admin.py

from django.contrib import admin

from pyweb.models import Publisher, Author, Book

admin.site.register(Publisher)

admin.site.register(Author)

admin.site.register(Book)

[root@LVS1 myweb]# ./manage.py syncdb

http://192.168.1.10:8000/admin/#用户名和密码为第一次执行python manage.py syncdb时创建的


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台