Python脚本之django---mysql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python脚本之django---mysql相关的知识,希望对你有一定的参考价值。

#######

安装python-MySQLdb

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

[[email protected] myweb]# pwd

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

[[email protected] 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

[[email protected] myweb]# pwd

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

[[email protected] myweb]# ./manage.py startapp pyweb

[[email protected] pyweb]# pwd

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

[[email protected] 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

[[email protected] myweb]# pwd

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

[[email protected] myweb]# python manage.py validate

0 errors found

[[email protected] myweb]# python manage.py sqlall pyweb

[[email protected] myweb]# python manage.py syncdb

[[email protected] 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站点

[[email protected] myweb]# pwd

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

[[email protected] 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‘

)

[[email protected] 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),

)

[[email protected] 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)

[[email protected] myweb]# ./manage.py syncdb


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










以上是关于Python脚本之django---mysql的主要内容,如果未能解决你的问题,请参考以下文章

Python脚本之socket

python脚本之paramiko

python脚本之日期格式显示

Python脚本之django---mysql

python脚本之与mysql交互

Python脚本之Multiprocessing 多线程