Django 1.5.4 专题一 入门
2013-10-02 19:31
579 查看
一.基本
如果需要安装sqlite数据库见http://blog.csdn.net/wcc526/article/details/12142967apt-get install python-setuptools easy_install virtualenv virtualenv --no-site-packages django-mike source django-mike/bin/activate cd django-mike/ easy_install Django django-admin.py startproject django_test python manage.py runserver 0.0.0.0:80
打开浏览器,输入ip,出现
如果要退出虚拟环境输入
deactivate vi settings.py 修改时区 TIME_ZONE='Asia/Shanghai' LANGUAGE_CODE = 'zh-cn'
python manage.py startapp article vi models.py
修改代码如下
from django.db import models class Article(models.Model): title =models.CharField(max_length=200) body =models.TextField() pub_date =models.DateTimeField('date published') likes =models.IntegerField() def __unicode__(self): return self.title
执行python manage.py syncdb
同步数据库,生成管理界面使用的额外的数据库表
可以使用SQLite Database Browser 查看sqlite 生成的数据表
注意django 会自动在表中自动插入一个id项作为primary key.
在settings.py 中添加APP
python manage.py sql article #生成sql代码并测试model python manage.py shell
vi article/views.py
from django.http import HttpResponse from django.shortcuts import render_to_response from django.views.generic.base import TemplateView def hello(request): name ="Mike" html =" <html> <body> Hi %s,this seems to have worked! </body> </html> " % name return HttpResponse(html) def hello_template_simple(request): name ="Mike" return render_to_response('article/hello.html',{'name':name}) class HelloTemplate(TemplateView): template_name='hello_class.html' def get_context_data(self,**kwargs): context=super(HelloTemplate,self).get_context_data(**kwargs) context['name']='Mike' return context
在settings.py 添加template路径
vi templates/hello.html,内容为以下,templates/hello_class.html的内容与其一致
路径为
vi urls.py
输入IP地址/hello,结果如下
相关文章推荐
- Django 1.5.4 专题14 Basic unit testing
- Django 1.5.4 专题20 fabric
- Django 1.5.4 专题24 Messages
- Django 1.5.4 专题26 Bootstrap
- Django 1.5.4 专题二 urls 和 view 提高
- Django 1.5.4 专题21 haystack whoosh
- Django 1.5.4 专题25 Notifications
- Django 1.5.4 专题15 south
- 一 Django 1.5.4 入门
- Django 1.5.4 专题18 Custom template tags
- Django 1.5.4 专题二 urls 和 view 提高
- Django 1.5.4 专题九 update database records
- Django 1.5.4 专题11 file uploading and updating database schema
- Django 1.5.4 专题19 heroku s3
- Django 1.5.4 专题26 Bootstrap
- Django 1.5.4 专题七 register
- Django 1.5.4 专题16 logging
- Django 1.5.4 专题十 database relationships
- Django 1.5.4 专题八 Model
- Django 1.5.4 专题12 Ajax search 功能