Django-1.3的helloworld和1.2.1下载

django各种版本的下载 比如1.2.1
http://code.djangoproject.com/svn/django/tags/releases/1.2.1/


1.安装python2.6后(2.7好像db插件没有exe版本的,不好装),下载Django-1.3.tar.gz
比如目录为E:\java\python\Django-1.3
2.python setup.py install之后会在C:\Python27\Lib\site-packages中建个目录django
设置E:\java\python\Python26\Lib\site-packages\django\bin到path环境变量
3.随便找个地方比如D:\java\python\workspace\work1>
运行django-admin.py startproject mysite
E:\java\python\workspace\work1\mysite>tree /F
文件夹 PATH 列表
卷序列号为 1ECF-E5A4
D:.
manage.py
settings.py
urls.py
__init__.py

没有子文件夹

工程名尽量不要用django或test等python内建的名称

python manage.py runserver

http://localhost:8000/

python manage.py runserver 8080 可以更改端口
python manage.py runserver 0.0.0.0:8080 可以更改ip,别人的机器,没试验

如果想连mysql数据库
配置setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': 'haoning',
'HOST': 'localhost',
'PORT': '3306',
}
}

再启动报错:django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
还需要装驱动,就像java的jar驱动一样
到http://www.djangoproject.com/r/python-mysql/ 下载MySQL-python-1.2.3.tar.gz
这个不好使,似乎还要装别的,改用MySQL-python-1.2.2.win32-py2.6.exe(http://sourceforge.net/projects/mysql-python/forums/forum/70460/topic/2316047?message=5613887)
附件中大的jpg改成exe使用,自动安装
参考http://pqcc.iteye.com/blog/511588 虽然和我的版本有点出入
把附件中的小的jpg去掉jpg后缀,改成rar,解压到两个dll文件放到E:\java\python\Python26\Lib\site-packages\MySQLdb\下,那个__init__.py修改
34行:
#from sets import ImmutableSet
#class DBAPISet(ImmutableSet):
class DBAPISet(frozenset):

去掉两行加一行
修改E:\java\python\Python26\Lib\site-packages\MySQLdb\converters.py
把两个大写变成小写
129行改成
set: Set2Str,
45行改
def Str2Set(s):
return set([ i for i in s.split(',') if i ])
去掉第37行:
#from sets import BaseSet, Set
如果python manage.py runserver还有问题,就删掉E:\java\python\Python26\Lib\site-packages\MySQLdb下面的所有pyc文件,只留py文件

E:\java\python\workspace\work1\mysite>python manage.py syncdb

将会在mysql中
E:\java\python\Python26\lib\site-packages\MySQLdb\__init__.py:34: DeprecationWarning: the sets module is deprecated
from sets import ImmutableSet
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site

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 'administrator'): root
E-mail address: haoningabc@163.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
No fixtures found.

E:\java\python\workspace\work1\mysite>

mysql:
mysql> show tables;
+----------------------------+
| Tables_in_test |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_message |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_content_type |
| django_session |
| django_site |
+----------------------------+
10 rows in set (0.00 sec)

mysql>

-----------------------------
工程创建完了,开始创建子应用app
python manage.py startapp polls

E:\java\python\workspace\work1\mysite>tree /F
卷 back 的文件夹 PATH 列表
卷序列号为 0A16-ED1C
E:.
│ manage.py
│ settings.py
│ settings.pyc
│ urls.py
│ urls.pyc
│ __init__.py
│ __init__.pyc

└─polls
models.py
tests.py
views.py
__init__.py


E:\java\python\workspace\work1\mysite>


修改models.py
from django.db import models

class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()

把poll模块加入到settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls'
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)

根据模块生成sql语句
E:\java\python\workspace\work1\mysite>python manage.py sql polls
E:\java\python\Python26\lib\site-packages\MySQLdb\__init__.py:34: DeprecationWarning: the sets module is deprecated
from sets import ImmutableSet
BEGIN;
CREATE TABLE `polls_poll` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`question` varchar(200) NOT NULL,
`pub_date` datetime NOT NULL
)
;
CREATE TABLE `polls_choice` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`poll_id` integer NOT NULL,
`choice` varchar(200) NOT NULL,
`votes` integer NOT NULL
)
;
ALTER TABLE `polls_choice` ADD CONSTRAINT `poll_id_refs_id_5d896c23` FOREIGN KEY (`poll_id`) REFERENCES `polls_poll` (`id`);
COMMIT;

可以刷到mysql里面;
验证
python manage.py validate
python manage.py sqlcustom polls
python manage.py sqlclear polls
python manage.py sqlindexes polls
python manage.py sqlall polls
这些都是查看sql语句

再运行[color=red]python manage.py syncdb[/color]就又在数据库里生成了
polls_choice,polls_poll 两个表

在有工程的环境变量下使用命令行
python manage.py shell
>>> import polls
>>> dir(polls)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'models']
>>>


django-admin.py help 查看帮助

已经看到E:\java\python\Django-1.3\docs\intro\tutorial01.txt的558行
下次继续吧
---------------mysql参考-----
http://www.iteye.com/topic/573092
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值