本篇博文介绍如何在PyCharm上用Django+MySQL开发个人博客。
首先是创建一个Django项目,并且使用 python manage.py startapp myblog 命令创建一个APP,并且在settings.py中添加blog,效果如下:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
接下来是数据库的操作:
首先,在MySQL里面创建一个叫做 blog 的数据库;然后在settings.py中加入数据库配置如下(用户名、密码进行替换):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blog',
'USER': '',
'PASSWORD':'',
'HOST':'localhost',
'PORT':'3306',
}
}
在models.py中放入如下代码作为数据库的描述:
# encoding:utf8
from django.db import models
from django.contrib import admin
class Article(models.Model):
title = models.CharField(max_length=100) # 博客题目
category = models.TextField(max_length=50, blank=True) # 博客标签
date_time = models.DateTimeField(auto_now_add=True) # 博客日期
content = models.TextField(blank=True, null = True) # 博客文章正文
def __unicode__(self):
return self.title
class Meta: # 按时间下降排序
ordering = ['-date_time']
# Register model
admin.site.register(Article)
运行如下命令同步数据库:
python manage.py migrate
python manage.py makemigrations
python manage.py migrate
最后是界面以及相应的数据库操作:
1、在settings.py中的TEMPLATES的DIRS位置修改如下:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR+"/templates",],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
2、在TEMPLATE目录下添加HTML文件,代码如下:
(1)base.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="A layout example that shows off a blog page with a list of posts.">
<title>Vincent's Blog</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/grids-responsive-min.css">
<link rel="stylesheet" href="http://picturebag.qiniudn.com/blog.css">
</head>
<body>
<div id="layout" class="pure-g">
<div class="sidebar pure-u-1 pure-u-md-1-4">
<div class="header">
<h1 class="brand-title">Vincent's Blog</h1><br>
<h2 class="brand-tagline">不想变成咸鱼的猫</h2><br>
<nav class="nav">
<ul class="nav-list">
<li class="nav-item">
<a class="pure-button" href="https://github.com/MrJoeyM">Github</a>
</li>
<li class="nav-item">
<a class="pure-button" href="http://blog.csdn.net/github_39611196">CSDN</a>
</li>
</ul>
</nav>
</div>
</div>
<div class="content pure-u-1 pure-u-md-3-4">
<div>
{% block content %}
{% endblock %}
<div class="footer">
<div class="pure-menu pure-menu-horizontal pure-menu-open">
<ul>
<li><a href="https://github.com/MrJoeyM">Github</a></li>
<li><a href="http://blog.csdn.net/github_39611196">CSDN</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
home.html
{% extends "base.html" %}
{% block content %}
<div class="posts">
{% for post in post_list %}
<section class="post">
<header class="post-header">
<h2 class="post-title">{{ post.title }}</h2>
<p class="post-meta">
Time: <a class="post-author" href="#">{{ post.date_time }}</a> <a class="post-category post-category-js" href="#">{{ post.category }}</a>
</p>
</header>
<div class="post-description">
<p>
{{ post.content }}
</p>
</div>
</section>
{% endfor %}
</div><!-- /.blog-post -->
{% endblock %}
3、在views.py中添加如下代码:
from blog.models import Article
from datetime import datetime
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def home(request):
post_list = Article.objects.all()
return render(request, 'home.html', {'post_list':post_list})
def detail(request, my_args):
post = Article.objects.all()[int(my_args)]
str = ("title = %s, category = %s, date_time = %s, content = %s"
% (post.title, post.category, post.date_time, post.content))
return HttpResponse(str)
4、修改urls.py为如下所示:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'blog.views.home'),
]
启动项目,在浏览器中输入 localhost:8000 即可看到如下效果:
最终效果如下图所示:
注:页面参考了一位博主的代码,如有侵权,一定删除
由于博客的介绍可能会有疏漏,所以如果不能运行成功,请参考GitHub上的项目。
附上GitHub链接
欢迎关注我的公众号:
编程技术与生活(ID:hw_cchang)