Djanjo1.11+MySQL5.7+Python2.7快速搭建博客

1.创建项目mysite

django-admin.py startproject mysite

2.运行开发服务器

python manage.py runserver

3.创建blog博客应用

python manage.py startapp blog

4.修改mysite/blog/models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

from django.db import models

from django import forms

# Create your models here.
class BlogPost(models.Model):
    title=models.CharField(max_length=150)
    body=models.TextField()
    timestamp=models.DateTimeField()
class BlogPostForm(forms.ModelForm):
    class Meta:
        model = BlogPost
        exclude = ('timestamp',)
class BlogPostAdmin(admin.ModelAdmin):
    list_display = ('title','body','timestamp')
admin.site.register(BlogPost,BlogPostAdmin)

5.设置数据库

修改setting.py文件中相应内容

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'testdb',
        'USER': 'root',
        'PASSWORD':'yue960713',
        'HOST':'',
        'POST':'',
    }
}

6.创建表

运行下面命令

Django 1.7.1及以上 用以下命令
# 1. 创建更改的文件
python manage.py makemigrations
# 2. 将生成的py文件应用到数据库
python manage.py migrate
 
 
旧版本的Django 1.6及以下用
python manage.py syncdb


7.创建用户界面

在blog下新建一个templates文件夹,将HTML文件放在这个文件夹下


     
{% csrf_token %}

{% for post in posts %}

{{ post.title }}

{{ post.timestamp }}

{{ post.body }}

{% endfor %}

8.配置url映射

修改mysite/urls.py文件

"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import include,url
from django.contrib import admin
from blog.views import *

urlpatterns = [
    url(r'^blog/$',archive),
    url(r'^admin/', admin.site.urls),
    url(r'^blog/create/',create_blogpost),
]

9.创建视图函数

修改blog/views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.http import HttpResponse

from blog.models import BlogPost

from datetime import datetime

from django.http import HttpResponseRedirect

from django.shortcuts import render

# Create your views here.

def archive(request):
    posts= BlogPost.objects.all().order_by('-timestamp')[:10]
    return render(request,'archive.html',{'posts':posts})
   
def create_blogpost(request):
    if request.method == 'POST':
        BlogPost(
            title=request.POST.get('title'),
            body=request.POST.get('body'),
            timestamp=datetime.now(),
        ).save()
    return HttpResponseRedirect('/blog/')

10.配置完成

在浏览器中输入http://localhost:8000/blog/验证一下

Djanjo1.11+MySQL5.7+Python2.7快速搭建博客_第1张图片

你可能感兴趣的:(python)