参考:《Django Web开发指南》
1、https://www.djangoproject.com/download/下载Django-1.5.1.tar.gz
2、在ubuntu下载解压:tar xzvf Django-1.5.1.tar.gz
3、切换到root,在Django-1.5.1/下执行 python setup.py install
4、安装在/usr/local/lib/python2.7/dist-packages/下
5、创建work目录,执行 django-admin.py startproject mysite
6、进入mysite目录,启动django服务器:
1)、python manage.py runserver
在浏览器中输入http://192.168.9.155:8080
2)、python manage.py runserver 0.0.0.0:8000
在浏览器中输入http://192.168.9.155:8000
可以看到相应页面了
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', # Uncomment the next line to enable the admin: 'django.contrib.admin', # Uncomment the next line to enable admin documentation: 'django.contrib.admindocs', )
from django.db import models # Create your models here. class BlogPost(models.Model): title = models.CharField(max_length= 150) body = models.TextField() timestamp = models.DateTimeField()
from django.contrib import admin admin.autodiscover()和
url(r'^admin/doc/', include('django.contrib.admindocs.urls')), url(r'^admin/', include(admin.site.urls)),的注释去掉
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'django', # Or path to database file if using sqlite3. # The following settings are not used with sqlite3: 'USER': 'johnny', 'PASSWORD': '0000', 'HOST': '192.168.9.155', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. 'PORT': '3306', # Set to empty string for default. } }注意:django数据库必须是已经建立好的数据库
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb解决办法:
Creating tables ... 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 django_site Creating table blog_blogpost 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 'johnny'): Email address: [email protected] Password: Password (again): Superuser created successfully. Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s)
#!/usr/bin/python # -*- coding: utf-8 -*- from django.contrib import admin from sblog.models import BlogPost admin.site.register(BlogPost)
from django.contrib import admin class BlogPostAdmin(admin.ModelAdmin): list_display = ('title','timestamp')在admin.py中修改:
from blog.models import BlogPost ,BlogPostAdmin admin.site.register(BlogPost,BlogPostAdmin)点击刷新页面,列表的title会显示你blog的标题,并显示时间戳;
{% for post in posts %} <h2>{{post.title}}</h2> <p>{{post.timestamp}}</p> <p>{{post.body}}</p> {% endfor %}b、创建视图函数:
from django.template import loader,Context from django.http import HttpResponse from blog.models import BlogPost def archive(request): posts = BlogPost.objects.all() t = loader.get_template("archive.html") c = Context({'posts':posts}) return HttpResponse(t.render(c))c、创建一个URL模式:
url(r'^blog/', include('blog.urls')),在blog目录中创建一个新的urls.py并编辑:
from django.conf.urls.defaults import * from blog.views import archive urlpatterns = patterns('', url(r'$',archive), )
{% block content %} <html> <style type="text/css"> body{color:#efd;background:#453;padding:0 5em;margin:0} h1{padding:2em 1em;background:#675} h2{color:#bf8;border-top:1px dotted #fff;margin-top:2em} p{margin: 1em 0} </style> <body> <h1>Johnny.He 的博客</h1> {% block content %} {% endblock %} </body> </html>
{% extends "base.html" %} {% block content %} {% for post in posts %} <h2>{{post.title}}</h2> <p>{{post.timestamp}}</p> <p>{{post.body}}</p> {% endfor %} {% endblock %}
class Meta: ordering = ('-timestamp',)刷新http://192.168.9.155:8000/blog/,就可以看到博客最新的贴子已经排到上面了