django

来源:互联网 发布:免费vpn翻墙软件 编辑:程序博客网 时间:2024/04/29 20:14

1.django-admin.py startproject mysite
2.python manage.py runserverpython manage.py runserver 8080

python manage.py runserver 0.0.0.0:8000

3.修改settings.py

DATABASES = {    'default': {        'ENGINE': 'django.db.backends.mysql',        # 'NAME': os.path.join(BASE_DIR, '/var/lib/mysql/lalala/db.opt'),        'NAME': 'mysql',        'USER': 'username',        'PASSWORD': 'password',        'HOST': '',    }}

‘NAME’一定要填数据库的名字,填注释掉的那行的时候出错:OperationalError: (1049, "Unknown database '/var/lib/mysql/lalala/db.opt'")

4.python manage.py syncdb出错 Error loading MySQLdb module: No module named MySQLdb解决:需要安装mysql-python,下载地址:http://sourceforge.net/projects/mysql-python/?source=dlp下载完,压缩,然后进入文件夹
python setup.py build出错,mysql_config not found原因:没有mysql_config这个文件,apt-get安装的MySQL是没有mysql_config这个文件的解决:(1)ubuntu下执行sudo apt-get install libmysqld-dev(2)fedora下执行 sudo yum install python-devel若出现:my_config.h:没有那个文件或目录,就执行:sudo yum install mysql-devel注意:yum也是python写的,默认是调用/usr/bin/python的,这是系统本身自带的python,自己安装的一般在/usr/local/bin/python,所以最好别把系统自带的python给卸载掉了,自己下载的python没有yum模块。
此时执行  find / -name mysql_config 或whereis mysql_config发现系统中有了mysql_config这个文件
 更改site.cfg中的设置,将mysql_config指向正确路径
再次python setup.py build出错:Python.h:没有那个文件或目录解决:安装python-dev,这是Python的头文件和静态库包:sudo apt-get install python-dev 
再次python setup.py build然后python setup.py installmysqldb的问题得以解决,证明:import MySQLdb无报错
python manage.py syncdb成功Creating table django_admin_logCreating table auth_permissionCreating table auth_group_permissionsCreating table auth_groupCreating table auth_user_groupsCreating table auth_user_user_permissionsCreating table auth_userCreating table django_content_typeCreating table django_session  
5.python manage.py startapp polls开始应用
6.修改settings.py:INSTALLED_APPS = (    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'polls',)TIME_ZONE = 'Asia/Shanghai'
7.python manage.py sql polls
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_text` varchar(200) NOT NULL,    `votes` integer NOT NULL);ALTER TABLE `polls_choice` ADD CONSTRAINT `poll_id_refs_id_3aa09835` FOREIGN KEY (`poll_id`) REFERENCES `polls_poll` (`id`);COMMIT;注:这些SQL命令并没有真的在Mysql中执行,只是打印出来告诉你Django需要些什么。可以拷贝这些命令到Mysql自己执行,当然Django提供了更好的方式。
一些命令:
  • python manage.py validate – Checks for any errors in the construction of your models.
  • python manage.py sqlcustom polls – Outputs any custom SQL statements (such as table modifications or constraints) that are defined for the application.
  • python manage.py sqlclear polls – Outputs the necessaryDROP TABLE statements for this app, according to which tables already exist in your database (if any).
  • python manage.py sqlindexes polls – Outputs theCREATE INDEX statements for this app.
  • python manage.py sqlall polls – A combination of all the SQL from the sqlsqlcustom, and sqlindexes commands.
8.python manage.py syncdb 再次。执行上面列出的那些SQL命令Creating tables ...Creating table polls_pollCreating table polls_choiceInstalling custom SQL ...Installing indexes ...Installed 0 object(s) from 0 fixture(s)
9.探索APIpython manage.py shell注:We’re using this instead of simply typing “python”, because manage.py sets the DJANGO_SETTINGS_MODULE environment variable, which gives Django the Python import path to your settings.py file.from polls.models import Poll, Choicefrom django.utils import timezonep = Poll(question="What's new?", pub_date=timezone.now())p.save()Poll.objects.all()Django models deal with Unicode by default.All data stored in your database is converted to Unicode when it’s returned.
10.编写views.py,修改urls.py
11.修改settings.py,增加:TEMPLATE_DIRS = (    '/home/zhyin/workspace/mysite/mysite/templates/',)
import os.pathTEMPLATE_DIRS = (    os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),)
from django.template.loader import get_templatefrom django.template import Contextt = get_template('current_datetime.html')
html = t.render(Context(data))
return HttpResponse(html)或from django.shortcuts import render_to_responsereturn render_to_response('current_datetime.html', data)

python manage.py createsuperuser创建另外的admin帐号