免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3314 | 回复: 0
打印 上一主题 下一主题

Django 学习记录(一) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-20 09:48 |只看该作者 |倒序浏览
1,在ubuntu环境下安装django:

sudo apt-get install python-django

 

2,安装python mysql相关的支持库文件:

sudo apt-get install mysql-server python-mysqldb

 

3,开始创建一个django的project:

django-admin startproject mysite

会在mysql目录下面自动生成四个文件:

mysite/
    __init__.py
    manage.py
    settings.py
    urls.py

4,首先配置settings.py中数据库相关的设置:(这里使用mysql为列)

修改数据库配置:

DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'dbname'             # Or path to database file if using sqlite3.
DATABASE_USER = 'dbuser'             # Not used with sqlite3.
DATABASE_PASSWORD = 'password'         # Not used with sqlite3.
DATABASE_HOST = '127.0.0.1'             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '3306'             # Set to empty string for default. Not used with sqlite3.

修改时区:

TIME_ZONE = 'Asia/Shanghai'

修改系统显示语言:

LANGUAGE_CODE = 'zh-cn'

在INSTALLED_APPS加入后台相关内容:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.admindocs',
)

5、同步数据库信息、并添加管理后台用户:

ubuntu:~/mysite$ python manage.py syncdb           
Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log

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 'zhaozhigang'): admin
E-mail address: admin@3cc.com.cn
Password:
Password (again):
Superuser created successfully.
Installing index for auth.Permission model
Installing index for auth.Message model
Installing index for admin.LogEntry model

6、设置urls.py开通后台管理访问设置:

ubuntu:~/mysite$ cat urls.py
from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from mysite.hello import hello
from mysite.currtime import *
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^mysite/', include('mysite.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
    # to INSTALLED_APPS 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)),
)

7,启动django的web服务:

ubuntu:~/mysite$ python manage.py runserver 0.0.0.0:8000
Validating models...
0 errors found

Django version 1.1.1, using settings 'mysite.settings'
Development server is running at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

[14/Jan/2011 23:23:28] "GET /admin/ HTTP/1.1" 200 1907
[14/Jan/2011 23:24:01] "GET /admin/doc/ HTTP/1.1" 200 2388

8、http://127.0.0.1/admin 即可登陆管理后台。

管理后台显示中文:

在setting.py中的MIDDLEWARE_CLASSES添加:

 'django.middleware.locale.LocaleMiddleware',

 

9、登陆管理后台提示:

You don't have permission to edit anything.

你无权修改任何东西。

的解决办法:在url.py总取消admin.autodiscover()前面的注释即可!

admin.autodiscover()
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP