Fork me on GitHub Fork me on Gitee

python + eclipse + django + postgresql 开发网站(二)

引用与参考 http://www.cnblogs.com/lanxuezaipiao/p/3283932.html

python2.7  django1.6

1.新建Django项目

  

  

  

 

2.创建网站模块app

  

 

3.测试新建的模块是否正常

  

服务器启动起来后,去浏览器输入网址:http://127.0.0.1:8000 

  

4.编辑代码

  4.1修改 MyBlog.models.py

 1 from django.db import models
 2 from django.contrib import admin
 3 
 4 # Create your models here.
 5 class BlogPost(models.Model):
 6     title = models.CharField(max_length = 150)
 7     content = models.TextField()
 8     timestamp = models.DateTimeField()
 9     
10 class BlogPostAdmin(admin.ModelAdmin):
11     list_display = ('title', 'content', 'timestamp')
12 
13 admin.site.register(BlogPost, BlogPostAdmin)

  

  4.2修改 MyBlog.views.py

 1 # Create your views here.
 2 from django.template import loader,Context
 3 from django.http import HttpResponse
 4 from MyBlog.models import BlogPost
 5 
 6 def archive(request):
 7     posts = BlogPost.objects.all()
 8     t = loader.get_template('archive.html')
 9     c = Context({'posts': posts})
10     return HttpResponse(t.render(c))

  

  4.3 修改MySiteWithPython.setting.py,找到下面部分进行修改

 1 INSTALLED_APPS = (
 2     'django.contrib.auth',
 3     'django.contrib.contenttypes',
 4     'django.contrib.sessions',
 5     'django.contrib.sites',
 6     'django.contrib.messages',
 7     'django.contrib.staticfiles',
 8     'MyBlog',
 9     # Uncomment the next line to enable the admin:
10     'django.contrib.admin',
11     # Uncomment the next line to enable admin documentation:
12     # 'django.contrib.admindocs',
13 )

  

  4.4 修改MySiteWithPython.urls.py

 1 from django.conf.urls import patterns, include, url
 2 
 3 # Uncomment the next two lines to enable the admin:
 4 from django.contrib import admin
 5 admin.autodiscover()
 6 
 7 from MyBlog.views import *
 8 
 9 urlpatterns = patterns('',
10     # Examples:
11     # url(r'^$', 'MySiteWithPython.views.home', name='home'),
12     # url(r'^MySiteWithPython/', include('MySiteWithPython.foo.urls')),
13 
14     # Uncomment the admin/doc line below to enable admin documentation:
15     # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
16 
17     # Uncomment the next line to enable the admin:
18     url(r'^admin/', include(admin.site.urls)),
19     url(r'^MyBlog/$', archive),
20 )

 

5.建立样式网页模板  

  请在包MyBlog下添加templates文件夹,并在templates下建立两个网页文件:archive.html和base.html

  5.1 编辑archive.html

1 {% extends "base.html" %}  
2 {% block content %}  
3 {% for post in posts %}  
4 <h1>{{ post.title}}</h1>  
5 <p>{{ post.content }}</p>
6 <p>{{ post.timestamp|date:"1, F jS"}}</p>
7 {% endfor %}  
8 {% endblock %}

  

  5.2 编辑base.html

 1 <html>  
 2   <style type="text/css">  
 3     body { color: #edf; background: #453; padding: 0 5em; margin:0 }  
 4     h1 { padding: 2em lem; background:#675 }  
 5     h2 { color: #bf8; border-top: 1px dotted #fff; margin-top: 2em }  
 6     p { margin: lem 0 }  
 7   </style>  
 8   <body>  
 9     <h1><center>Alexia's Blog</center></h1>  
10     {% block content %}  
11     {% endblock %}  
12   </body>  
13 </html>

 

6.同步数据库

  

 

  设置你的账号和密码,为登陆blog的管理后台作准备。

 

  

7.运行测试

  登陆界面,登陆账号和密码是初始化数据库的时候设定的。

  

  登录成功后跳转到下面页面:

  

  在该页面可以添加blog文章:

  

 

  发布成功后,输入网址:http://127.0.0.1:8000/MyBlog/进行查看,测试成功!

posted @ 2015-08-26 11:19  伴途の永远  阅读(489)  评论(0编辑  收藏  举报