Django+python+eclipse 快速搭建博客blog

本文详细介绍了如何使用Django框架从零开始搭建一个简单的个人博客网站,包括项目的创建、数据库配置、模型定义、视图编写、URL配置及模板设计等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.新建Django项目





选择sqlite数据库



2.创建博客模块app






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





4.编辑代码


4.1修改 blog.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.     body = models.TextField() 
  8.     timestamp = models.DateTimeField() 
  9.  
  10. class BlogPostAdmin(admin.ModelAdmin): 
  11.     list_display = ('title','body','timestamp'
  12.  
  13. admin.site.register(BlogPost,BlogPostAdmin) 
from django.db import models
from django.contrib import admin

# Create your models here.
class BlogPost(models.Model):
    title = models.CharField(max_length=150)
    body = models.TextField()
    timestamp = models.DateTimeField()

class BlogPostAdmin(admin.ModelAdmin):
    list_display = ('title','body','timestamp')

admin.site.register(BlogPost,BlogPostAdmin)

4.2修改 blog.views.py

  1. # Create your views here. 
  2. from django.template import loader,Context 
  3. from django.http import HttpResponse 
  4. from blog.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)) 
# Create your views here.
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))

4.3 修改mysite.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.     'blog'
  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', 
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',
)

4.4 修改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. from blog.views import
  7.  
  8. urlpatterns = patterns(''
  9.     # Examples: 
  10.     # url(r'^$', 'mysite.views.home', name='home'), 
  11.     # url(r'^mysite/', include('mysite.foo.urls')), 
  12.  
  13.     # Uncomment the admin/doc line below to enable admin documentation: 
  14.     # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 
  15.  
  16.     # Uncomment the next line to enable the admin: 
  17.      url(r'^admin/', include(admin.site.urls)), 
  18.      url(r'^blog/$', archive), 
from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
from blog.views import *

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^mysite/', include('mysite.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
     url(r'^admin/', include(admin.site.urls)),
     url(r'^blog/$', archive),
)

5.建立样式网页模板


注意:请在模块blog下添加templates文件夹



5.1 编辑archive.html

  1. {% extends "base.html" %} 
  2. {% block content %} 
  3. {% for post in posts %} 
  4. <h1>{{ post.title}}</h1> 
  5. <p>{{ post.timestamp|date:"1, F jS"}}</p> 
  6. <p>{{ post.body }}</p> 
  7. {% endfor %} 
  8. {% endblock %} 
{% extends "base.html" %}
{% block content %}
{% for post in posts %}
<h1>{{ post.title}}</h1>
<p>{{ post.timestamp|date:"1, F jS"}}</p>
<p>{{ post.body }}</p>
{% endfor %}
{% 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>mysite.example.com</h1> 
  10. {% block content %} 
  11. {% endblock %} 
  12. </body> 
  13. </html> 
<html>
<style type="text/css">
body { color: #edf; background: #453; padding: 0 5em; margin:0 }
h1 { padding: 2em lem; background:#675 }
h2 { color: #bf8; border-top: 1px dotted #fff; margin-top: 2em }
p { margin: lem 0 }
</style>
<body>
<h1>mysite.example.com</h1>
{% block content %}
{% endblock %}
</body>
</html>

6.同步数据库



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




7.运行成功




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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值