Chinaunix首页 | 论坛 | 博客
  • 博客访问: 60163
  • 博文数量: 38
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 396
  • 用 户 组: 普通用户
  • 注册时间: 2015-07-12 14:46
文章分类

全部博文(38)

文章存档

2016年(11)

2015年(27)

我的朋友
最近访客

分类: Python/Ruby

2016-08-28 12:40:46



创建一个项目:
[root@xiaofan63 python]# django-admin.py startproject web
[root@xiaofan63 python]# ls
web
[root@xiaofan63 python]# cd web/
[root@xiaofan63 web]# ls
manage.py  web
[root@xiaofan63 web]# cd web/
[root@xiaofan63 web]# ls
__init__.py  settings.py  urls.py  wsgi.py




启动一个web服务器:
[root@xiaofan63 web]# ls
manage.py  web
[root@xiaofan63 web]# python manage.py runserver 0.0.0.0:9000
Validating models...
0 errors found
August 21, 2016 - 02:52:34
Django version 1.6.11, using settings 'web.settings'
Starting development server at
Quit the server with CONTROL-C.
[21/Aug/2016 02:52:56] "GET / HTTP/1.1" 200 1757


访问:
[root@xiaofan63 ~]# curl -I 192.168.1.63:9000
HTTP/1.0 200 OK
Date: Sun, 21 Aug 2016 02:54:22 GMT
Server: WSGIServer/0.1 Python/2.6.6
X-Frame-Options: SAMEORIGIN
Content-Type: text/html


-----------------------------


添加一个应用:


[root@xiaofan63 web]# ls
manage.py  web


[root@xiaofan63 web]# python manage.py startapp blog
[root@xiaofan63 web]# ls
blog  manage.py  web
[root@xiaofan63 web]# cd blog/
[root@xiaofan63 blog]# ll
total 16
-rw-r--r-- 1 root root 63 Aug 21 10:56 admin.py       #管理用
-rw-r--r-- 1 root root  0 Aug 21 10:56 __init__.py
-rw-r--r-- 1 root root 57 Aug 21 10:56 models.py     #M,数据存取部分
-rw-r--r-- 1 root root 60 Aug 21 10:56 tests.py     #测试用  
-rw-r--r-- 1 root root 63 Aug 21 10:56 views.py    #V,视图和模板




[root@xiaofan63 web]# pwd
/python/web/web
[root@xiaofan63 web]# ls
__init__.py   settings.py   urls.py (#C,rul设置)  wsgi.py
__init__.pyc  settings.pyc  urls.pyc  wsgi.pyc


加入应用:
[root@xiaofan63 web]# vim settings.py
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
)


修改url配置文件:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^blog/index/$','blog.views.index'),
)




[root@xiaofan63 blog]# pwd
/python/web/blog
[root@xiaofan63 blog]# vim views.py 
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
    return HttpResponse('hello world')


通过浏览器访问测试结果.


----------------------------------


添加网页文件地址:
[root@xiaofan63 blog]# pwd
/python/web/blog
[root@xiaofan63 blog]# mkdir templates   #默认templates网页文件地址目录,如需修改要在setting文件中修改
[root@xiaofan63 blog]# cd templates/
[root@xiaofan63 templates]# vim index.html

hello




[root@xiaofan63 blog]# pwd
/python/web/blog
[root@xiaofan63 blog]# vim views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader,Context
# Create your views here.
def index(request):
    t = loader.get_template('index.html')
    c = Context({})
    return HttpResponse(t.render(c))
#    return HttpResponse('hello world')


通过浏览器访问测试结果.




------------------------------------
数据类型:


定义数据类表:
[root@xiaofan63 blog]# pwd
/python/web/blog
[root@xiaofan63 blog]# vim models.py
from django.db import models
# Create your models here.
class Host(models.Model):
    hostname = models.CharField(max_length=50)
    ip = models.IPAddressField()


更新数据库:
[root@xiaofan63 web]# pwd
/python/web
[root@xiaofan63 web]# python manage.py validate
0 errors found
[root@xiaofan63 web]# grep "DATABASE" -A5 web/settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
[root@xiaofan63 ~]# rpm -q sqlite
sqlite-3.6.20-1.el6.x86_64
[root@xiaofan63 web]# pwd
/python/web
[root@xiaofan63 web]# ls
blog  manage.py  web


查看syncdb要执行什么:
[root@xiaofan63 web]# python manage.py sqlall blog
BEGIN;
CREATE TABLE "blog_host" (
    "id" integer NOT NULL PRIMARY KEY,
    "hostname" varchar(50) NOT NULL,
    "ip" char(15) NOT NULL
)
;


COMMIT;


同步数据库:
[root@xiaofan63 web]# python manage.py syncdb
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table blog_host


You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'root'): root
Email address: root@123.com
Password: 123456
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)


[root@xiaofan63 web]# ls
blog  db.sqlite3  manage.py  web


进入sqlite数据库:
[root@xiaofan63 web]# sqlite3 db.sqlite3 
SQLite version 3.6.20
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
auth_group                  auth_user_user_permissions
auth_group_permissions      blog_host                 
auth_permission             django_admin_log          
auth_user                   django_content_type       
auth_user_groups            django_session            
sqlite> select * from auth_user;
1|pbkdf2_sha256$12000$gYGRlvVRRumF$UH6NRmm5vp+leFtRqorQbESdZq6r2eb/0ZcZKMN99Hc=|2016-08-21 03:46:51.713980|1|root|||root@123.com|1|1|2016-08-21 03:46:51.713980
sqlite> .exit


[root@xiaofan63 web]# python manage.py dbshell
SQLite version 3.6.20
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .exit




---------------------
把表注册到admin中:
[root@xiaofan63 blog]# pwd
/python/web/blog
[root@xiaofan63 blog]# vim admin.py
from django.contrib import admin
from blog.models import Host
# Register your models here.
class HostAdmin(admin.ModelAdmin):
    list_display = ['hostname','ip']


admin.site.register(Host,HostAdmin)
~                                                           
~    


通过网页访问django管理页面:





-----------------------------------------
django访问数据库:


通过python访问:
[root@xiaofan63 web]# python manage.py shell
Python 2.6.6 (r266:84292, Sep  4 2013, 07:46:00) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 
>>> from blog.models import Host
>>> node=Host.objects.all()
>>> node.values()
[{'ip': u'192.168.1.63', 'hostname': u'node01', u'id': 1}]
>>> n=Host(hostname='node02',ip='192.168.1.66')
>>> n.save()
>>> nodes=Host.objects.all()
>>> nodes.values()
[{'ip': u'192.168.1.63', 'hostname': u'node01', u'id': 1}, {'ip': u'192.168.1.66', 'hostname': u'node02', u'id': 2}]
>>> 
>>> n=Host()
>>> n.hostname='node03'
>>> n.ip='192.168.1.67'
>>> n.save()
>>> nodes=Host.objects.all()
>>> nodes.values()
[{'ip': u'192.168.1.63', 'hostname': u'node01', u'id': 1}, {'ip': u'192.168.1.66', 'hostname': u'node02', u'id': 2}, {'ip': u'192.168.1.67', 'hostname': u'node03', u'id': 3}]


通过views访问:


添加url:
[root@xiaofan63 web]# pwd
/python/web/web
[root@xiaofan63 web]# vim urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^blog/index/$','blog.views.index'),
    url(r'^db/$','blog.views.db'),
)






[root@xiaofan63 blog]# pwd
/python/web/blog
[root@xiaofan63 blog]# vim views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader,Context
from blog.models import Host
# Create your views here.
def index(request):
    t = loader.get_template('index.html')
    c = Context({})
    return HttpResponse(t.render(c))
#    return HttpResponse('hello world')
def db(req):
    h = Host()
    h.hostname = 'node04'
    h.ip = '192.168.1.70'
    h.save()
    return HttpResponse('OK')


测试访问:

blog/host/
阅读(604) | 评论(0) | 转发(0) |
0

上一篇:网络通讯

下一篇:没有了

给主人留下些什么吧!~~