用html页面模板使用django完成个人博客

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

⽤html页⾯模板使⽤django完成个⼈博客1、进⼊虚拟环境: workon 虚拟环境名
2、找到我们的项⽬管理⽂件夹django,进⼊创建项⽬django-admin startproject blog
3、进⼊到我们的项⽬⽂件夹当中,创建我们的第⼀个应⽤ python manage.py startapp user ⽤于⽤户管理
创建我们的第⼆个应⽤python manage.py startapp articles ⽤于⽂章管理
4、使⽤pycharm打开创建的项⽬blog,在pycharm当中设置项⽬的虚拟环境
5、由于我们在创建项⽬的过程中会遇到两个或者以上的应⽤,为了便于管理,我们⼀般创建⼀个新的⽂件夹(apps)⽤于管理所有的应⽤:
6、然后将apps设置成为根⽬录:
8、设置settings
"""
Django settings for blog project.
Generated by 'django-admin startproject' using Django 1.8.2.
For more information on this file, see
https:///en/1.8/topics/settings/
For the full list of settings and their values, see
https:///en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os,sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0,'apps') #⽬的是将⽂件夹apps设置为根⽬录
# Quick-start development settings - unsuitable for production
# See https:///en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'i)hua049@1k6g-mf=b%h3-g8ma&#$i#*al8zu%gg!z-l8o_rz2'
# SECURITY WARNING: don't run with debug turned on in production! DEBUG = True # True为调试模式,Fasle为⽣产模式
ALLOWED_HOSTS = []
# Application definition
#⾥⾯写⼊应⽤名称
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
'articles',
)
AUTH_USER_MODEL = 'erProfile'
#中间键设置,⼀般默认的就⾏了
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'monMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'mymiddlewears.HandlerRequestMiddleWare'
)
ROOT_URLCONF = 'blog.urls'
#设置静态⽂件配置,⼀般会创建⼀个templates⽂件夹⽤于存放静态⽂件TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
'django.template.context_processors.media',#⽤于图⽚的处理
],
},
},
]
WSGI_APPLICATION = 'blog.wsgi.application'
# Database
# https:///en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', #处理器使⽤的数据库,这⾥是mysql 'NAME': 'blog', #mysql数据库名字
'USER': 'root', #mysql数据库⽤户
'PASSWORD': 'root',#密码
'HOST': 'localhost',
'PORT': '3306',
}
}
LANGUAGE_CODE = 'zh-CN'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False
# Static files (CSS, JavaScript, Images)
# https:///en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
#配置媒体⽂件夹
MEDIA_URL = '/static/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'static/media')
9、再到mysql数据库⾥创建数据库blog。

10、blog中urls代码:
"""blog URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https:///en/1.8/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. Add an import: from blog import urls as blog_urls
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 django.views.static import serve
from users.views import index
from blog.settings import MEDIA_ROOT
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^users/',include('users.urls',namespace='users')),
url(r'^articles/',include('articles.urls',namespace='articles')),
url(r'^$',index,name='index'),
url(r'^static/media/(?P<path>.*)',serve,{'document_root':MEDIA_ROOT})
]
11、user 中forms.py中代码:
from django import forms
class UserRegistForm(forms.Form):
username=forms.CharField(max_length=20,min_length=6,required=True,error_messages={ 'max_length':'⽤户名最⼤长度为20',
'min_length':'⽤户名最⼩长度为6',
'required':'⽤户名为必填'
})
email=forms.EmailField(max_length=100,min_length=8,required=False,error_messages={ 'invalid':'邮箱格式为:xxx@'
})
url=forms.URLField(max_length=100,min_length=8,required=False,error_messages={ 'invalid':'⽹址格式为:'
})
password=forms.CharField(max_length=20,min_length=8,required=True,error_messages={ 'max_length':'密码最⼤长度为20',
'min_length':'密码名最⼩长度为8',
'required':'密码为必填'
})
password1=forms.CharField(max_length=20,min_length=8,required=True,error_messages={ 'max_length':'密码最⼤长度为20',
'min_length':'密码名最⼩长度为8',
'required':'密码为必填'
})
class UserloginForm(forms.Form):
username=forms.CharField(max_length=20,min_length=6,required=True)
password=forms.CharField(max_length=20,min_length=8,required=True)
12、user 中models.py中代码:
from django.db import models
from django.contrib.auth.models import AbstractUser
from datetime import datetime
# Create your models here.
class UserProfile(AbstractUser):
nick_name=models.CharField(max_length=20,verbose_name='⽤户昵称',null=True,blank=True) url=models.URLField(max_length=100,verbose_name='⽤户主页',null=True,blank=True)
add_time=models.DateTimeField(default=datetime.now,verbose_name='添加时间')
def__str__(self):
return ername
class Meta:
verbose_name='⽤户信息'
verbose_name_plural=verbose_name
完成后注意⽣成迁移⽂件命令:python3 manage.py makemigrations
执⾏sql语句⽣成数据表命令:python3 manage.py migrate
13、user 中urls.py中代码:
"""blog URL Configuration
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. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from .views import user_regist,user_login,user_logout
urlpatterns = [
url(r'^user_regist/$',user_regist,name='user_regist'),
url(r'^user_login/$',user_login,name='user_login'),
url(r'^user_logout/$',user_logout,name='user_logout'),
]
14、user 中views.py中代码:
from django.shortcuts import render,redirect
from django.core.urlresolvers import reverse
from .forms import UserRegisterForm,UserLoginForm
from .models import UserProfile
from django.contrib.auth import authenticate,logout,login
from articles.models import ArticleInfo,TagInfo
# Create your views here.
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
def index(request):
all_articles = ArticleInfo.objects.all()
#在django内部orm模型查询集上可以⽀持排序和切⽚,但是切⽚不能是负索引
#浏览排⾏
date_time = all_articles.datetimes('add_time','day',order='DESC')
click_sort = all_articles.order_by('-click_num')[:6]
#站长推荐
pro_arts = all_articles.order_by('-add_time')[:6]
all_tags = TagInfo.objects.all()
year = request.GET.get('year','')
month = request.GET.get('month','')
day = request.GET.get('day','')
tagid = request.GET.get('tagid', '')
if year and month and day:
all_articles = all_articles.filter(add_time__year=year,add_time__month=month,add_time__day=day) all_articles_set = set(all_articles)
if tagid:
tag = TagInfo.objects.filter(id=int(tagid))[0]
all_articles = tag.article.all()
all_articles_set1 = set(all_articles)
# all_articles = [article for article in all_tag_articles if article in all_articles]
try:
a = list(all_articles_set & all_articles_set1)
if a:
all_articles = a
except:
pass
pa = Paginator(all_articles,2)
pagenum = request.GET.get('pagenum',1)
try:
pages = pa.page(pagenum)
except PageNotAnInteger:
pages = pa.page(1)
except EmptyPage:
pages = pa.page(pa.num_pages)
return render(request,'index.html',{
# 'all_articles':all_articles
'pages':pages,
'click_sort':click_sort,
'pro_arts':pro_arts,
'all_tags':all_tags,
'tagid':tagid,
'date_time':date_time,
'year':year,
'month':month,
'day':day
})
def user_register(request):
if request.method == 'GET':
return render(request,'reg.html')
else:
#实例化form类,⽤来验证⽤户提交的数据
user_register_form = UserRegisterForm(request.POST)
#⼀个判断⽅法:判断这个form验证是否通过(合法),如果合法返回True,不合法返回False
if user_register_form.is_valid():
#如果验证合法,那么会把合法的⼲净的数据存储在form对象的⼀个属性cleaned_data
#当中,这个属性是⼀个字典,我们可以这样去拿⼲净的数据
username = user_register_form.cleaned_data['username']
email = user_register_form.cleaned_data['email']
url = user_register_form.cleaned_data['url']
password = user_register_form.cleaned_data['password']
password1 = user_register_form.cleaned_data['password1']
user = UserProfile.objects.filter(username=username)
if user:
return render(request,'reg.html',{
'msg':'帐号已经存在'
})
else:
if password == password1:
a = UserProfile()
ername =username
a.email = email
a.url = url
a.password = password
a.set_password(password)
a.save()
return render(request, 'reg.html', {
'user_register_form': user_register_form
})
def user_login(request):
if request.method == 'GET':
return render(request,'login.html')
else:
user_login_form = UserLoginForm(request.POST)
if user_login_form.is_valid():
username = user_login_form.cleaned_data['username']
password = user_login_form.cleaned_data['password']
user = authenticate(username = username,password = password)
if user:
login(request,user)
return redirect(reverse('index'))
else:
return render(request,'login.html',{
'msg':'⽤户名或者密码错误'
})
else:
return render(request, 'login.html', {
'user_login_form': user_login_form
})
def user_logout(request):
logout(request)
return redirect(reverse('index'))
15、articles 中admin.py中代码:
from django.contrib import admin
from .models import ArticleInfo,Category,TagInfo,CommentInfo
# Register your models here.
# Create your models here.
class CategoryAdmin(admin.ModelAdmin):
list_display = ['name','add_time']
fields = ['name','add_time']
class ArticleInfoAdmin(admin.ModelAdmin):
list_display = ['title', 'author','desc','content','is_delete','click_num','love_num','image','add_time','category'] fields = ['title', 'author','desc','content','is_delete','click_num','love_num','image','add_time','category'] class TagInfoAdmin(admin.ModelAdmin):
list_display = ['name', 'add_time']
fields = ['name', 'add_time','article']
filter_horizontal = ['article']
class CommentInfoAdmin(admin.ModelAdmin):
list_display = ['comment_man', 'add_time','comment_art','comment_content','is_delete']
fields = ['comment_man', 'add_time','comment_art','comment_content','is_delete']
admin.site.register(Category,CategoryAdmin)
admin.site.register(ArticleInfo,ArticleInfoAdmin)
admin.site.register(TagInfo,TagInfoAdmin)
admin.site.register(CommentInfo,CommentInfoAdmin)
16、articles 中models.py中代码:
from django.db import models
from datetime import datetime
from users.models import UserProfile
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=20,verbose_name="⽂章类别")
add_time = models.DateTimeField(default=datetime.now,verbose_name="添加时间")
def__str__(self):
return
class Meta:
verbose_name = "类别信息"
verbose_name_plural = verbose_name
class ArticleInfo(models.Model):
title = models.CharField(max_length=50,verbose_name="⽂章标题")
author = models.ForeignKey(UserProfile,verbose_name='⽂章作者')
category = models.ForeignKey(Category,verbose_name="所属类别",null=True,blank=True)
desc = models.CharField(max_length=200,verbose_name="⽂章摘要")
content = models.TextField(verbose_name="⽂章内容")
is_delete = models.BooleanField(default=False,verbose_name="是否删除")
click_num = models.IntegerField(default=0,verbose_name="浏览量")
love_num = models.IntegerField(default=0,verbose_name="点赞数")
image = models.ImageField(max_length=200,verbose_name="⽂章图⽚",upload_to='article/%y/%m/%d') add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
def__str__(self):
return self.title
class Meta:
verbose_name = '⽂章信息'
verbose_name_plural = verbose_name
class TagInfo(models.Model):
name = models.CharField(max_length=20,verbose_name='标签名称')
article = models.ManyToManyField(ArticleInfo,verbose_name="所属⽂章")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
def__str__(self):
return
class Meta:
verbose_name = "标签信息"
verbose_name_plural = verbose_name
class CommentInfo(models.Model):
def__str__(self):
return ment_content
class Meta:
verbose_name = "评论信息"
verbose_name_plural = verbose_name
完成后注意⽣成迁移⽂件命令:python3 manage.py makemigrations
执⾏sql语句⽣成数据表命令:python3 manage.py migrate
17、articles 中urls.py中代码:
"""blog URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https:///en/1.8/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. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from .views import article_detail,comment_add,love_add,article_add
urlpatterns = [
url(r'^article_detail/(\d+)/$',article_detail,name='article_detail'),
url(r'^article_add/$', article_add, name='article_add'),
url(r'^comment_add/(\d+)/$',comment_add,name='comment_add'),
url(r'^love_add/(\d+)/$', love_add, name='love_add'),
]
18、articles 中views.py中代码:
from django.shortcuts import render,HttpResponse,redirect
from django.core.urlresolvers import reverse
from .models import ArticleInfo,TagInfo,Category,CommentInfo
from django.core.paginator import PageNotAnInteger,Paginator,EmptyPage
from django.contrib.auth.decorators import login_required
# import json
from django.http import JsonResponse
# Create your views here.
from blog.settings import MEDIA_ROOT
import os
def article_detail(request,art_id):
if art_id:
article = ArticleInfo.objects.filter(id=int(art_id))[0]
article.click_num += 1
article.save()
all_articles = ArticleInfo.objects.all()
# 在django内部orm模型查询集上可以⽀持排序和切⽚,但是切⽚不能是负索引
# 浏览排⾏
date_time = all_articles.datetimes('add_time', 'day', order='DESC')
click_sort = all_articles.order_by('-click_num')[:6]
# 站长推荐
pro_arts = all_articles.order_by('-add_time')[:6]
all_tags = TagInfo.objects.all()
year = request.GET.get('year', '')
month = request.GET.get('month', '')
day = request.GET.get('day', '')
tagid = request.GET.get('tagid', '')
if year and month and day:
all_articles = all_articles.filter(add_time__year=year, add_time__month=month, add_time__day=day) all_articles_set = set(all_articles)
if tagid:
tag = TagInfo.objects.filter(id=int(tagid))[0]
all_articles = tag.article.all()
all_articles_set1 = set(all_articles)
# all_articles = [article for article in all_tag_articles if article in all_articles]
try:
a = list(all_articles_set & all_articles_set1)
if a:
all_articles = a
except:
pass
pa = Paginator(all_articles, 2)
pagenum = request.GET.get('pagenum', 1)
try:
pages = pa.page(pagenum)
except PageNotAnInteger:
pages = pa.page(1)
except EmptyPage:
pages = pa.page(pa.num_pages)
return render(request,'article.html',{
'article':article,
'pages': pages,
'click_sort': click_sort,
'pro_arts': pro_arts,
'all_tags': all_tags,
'tagid': tagid,
'date_time': date_time,
'year': year,
'month': month,
'day': day
})
# def article_add(request):
# if request.method == "GET":
# all_category = Category.objects.all()
# return render(request,'article_add.html',{
# tag = request.POST.get('tag')
# category = request.POST.get('category')
# content = request.POST.get('content')
#
# cat = Category.objects.filter(name=category)[0]
# art = ArticleInfo()
# art.title = title
# art.desc = desc
# art.content = content
# art.image = 'article/'+
# art.author_id = er.id
# art.category_id = cat.id
# art.save()
#
# tg = TagInfo()
# = tag
# tg.save()
#
# tg.article.add(art)
#
# file_name = os.path.join(MEDIA_ROOT,str(art.image))
# with open(file_name,'wb') as f:
# for c in image.chunks():
# f.write(c)
# return redirect(reverse('index'))
@login_required(login_url='/users/user_login/')
def comment_add(request,art_id):
if er:
if art_id:
content = request.POST.get('comment','')
com = CommentInfo()
ment_man_id = er.id
ment_art_id = int(art_id)
ment_content = content
com.save()
return redirect(reverse('articles:article_detail',args=[art_id]))
def love_add(request,art_id):
if request.is_ajax():
art = ArticleInfo.objects.filter(id=int(art_id))[0]
art.love_num += 1
art.save()
result = {'a':'ok'}
return JsonResponse(result)
def article_add(request):
if request.method == "GET":
all_category = Category.objects.all()
return render(request,'article_add.html',{
'all_category':all_category
})
else:
arttitle = request.POST.get('arttitle','')
artdesc = request.POST.get('artdesc','')
artimage = request.FILES.get('artimage','')
artcategory = request.POST.get('artcategory','')
arttag = request.POST.get('arttag','')
artcontent = request.POST.get('artcontent','')
cat = Category.objects.filter(name=artcategory)[0]
art = ArticleInfo()
art.title = arttitle
art.desc = artdesc
art.image = 'article/'+
art.content = artcontent
art.category_id = cat.id
art.author_id = er.id
art.save()
tag = TagInfo()
= arttag
tag.save()
tag.article.add(art)
file_name = os.path.join(MEDIA_ROOT,str(art.image))
with open(file_name,'wb') as f:
for c in artimage.chunks():
f.write(c)
return redirect(reverse('index'))
19、在static⽂件中导⼊css,js,images等模板静态⽂件
20、templates⽂件中base.htms表⽰⽗模板,可以让其它与之类似的页⾯继承: <!doctype html>
{% load staticfiles %}
<html>
<head>
<meta charset="utf-8">
<title>某某某的个⼈博客</title>
<meta name="keywords" content=""/>
<meta name="description" content=""/>
<link href='{% static 'css/base.css' %}' rel="stylesheet">
<link href='{% static 'css/index.css' %}' rel="stylesheet">
{% block mycss %}{% endblock %}
<script type="text/javascript" src='{% static 'js/jquery.min.js' %}'></script>
<script type="text/javascript" src='{% static 'js/sliders.js' %}'></script>
<!--[if lt IE 9]>
<script src="{% static 'js/modernizr.js' %}"></script>
<![endif]-->
</head>
<body>
<header>
<div class="logo">
{% else %}
<ul>
<li><a href="{% url 'users:user_login' %}">登录</a></li>
<li><a href="{% url 'users:user_register' %}" style="border-left: 1px solid black">注册</a></li>
</ul>
{% endif %}
</div>
<nav id="topnav" class="f_r">
<ul>
<a href="index.html" target="_blank">⾸页</a><a href="news.html" target="_blank">关于我</a><a href="p.html" target="_blank">⽂章</a><a href="a.html" target="_blank">⼼情</a><a href="c.html" target="_blank">相册</a </ul>
<script src="{% static 'js/nav.js' %}"></script>
</nav>
</header>
<article>
{% block left %}
{% endblock %}
<div class="r_box f_r">
<div class="tit01">
<h3>关注我</h3>
<div class="gzwm">
<ul>
<li><a class="xlwb" href="#" target="_blank">新浪微博</a></li>
<li><a class="txwb" href="#" target="_blank">腾讯微博</a></li>
<li><a class="rss" href="portal.php?mod=rss" target="_blank">RSS</a></li>
<li><a class="wx" href="mailto:admin@">邮箱</a></li>
</ul>
</div>
</div>
<!--tit01 end-->
<div class="moreSelect" id="lp_right_select">
<script>
window.onload = function ()
{
var oLi = document.getElementById("tab").getElementsByTagName("li");
var oUl = document.getElementById("ms-main").getElementsByTagName("div");
for(var i = 0; i < oLi.length; i++)
{
oLi[i].index = i;
oLi[i].onmouseover = function ()
{
for(var n = 0; n < oLi.length; n++) oLi[n].className="";
this.className = "cur";
for(var n = 0; n < oUl.length; n++) oUl[n].style.display = "none";
oUl[this.index].style.display = "block"
}
}
}
</script>
<div class="ms-top">
<ul class="hd" id="tab">
<li class="cur"><a href="/">浏览排⾏</a></li>
<li><a href="/">评论排⾏</a></li>
<li><a href="/">站长推荐</a></li>
</ul>
</div>
<div class="ms-main" id="ms-main">
<div style="display: block;" class="bd bd-news">
<ul>
{% for sort in click_sort %}
<li><a href="/" target="_blank">{{ sort.title }}</a></li>
{% endfor %}
</ul>
</div>
<div class="bd bd-news">
<ul>
<li><a href="/" target="_blank">原来以为,⼀个⼈的勇敢是,删掉他的⼿机号码...</a></li>
<li><a href="/" target="_blank">⼿机的16个惊⼈⼩秘密,据说99.999%的⼈都不知</a></li>
<li><a href="/" target="_blank">住在⼿机⾥的朋友</a></li>
<li><a href="/" target="_blank">教你怎样⽤⽋费⼿机拨打电话</a></li>
<li><a href="/" target="_blank">你⾯对的是⽣活⽽不是⼿机</a></li>
<li><a href="/" target="_blank">豪雅⼿机正式发布! 在法国全⼿⼯打造的奢侈品</a></li>
</ul>
</div>
<div class="bd bd-news">
<ul>
{% for pro in pro_arts %}
<li><a href="/" target="_blank">{{ pro.title }}</a></li>
{% endfor %}
</ul>
</div>
</div>
<!--ms-main end -->
</div>
<!--切换卡 moreSelect end -->
<div class="cloud">
<h3>标签云</h3>
<ul>
{% for tag in all_tags %}
<li><a href="{% url 'index' %}?tagid={{ tag.id }}&pagenum={{ pages.number }}&year={{ year }}&month={{ month }}&day={{ day }}">{{ }}</a></li>
{% endfor %}
</ul>
</div>
<div class="tuwen">
<h3>⽂章归档</h3>
<ul>
{% for date in date_time %}
<li>
<p><span class="tutime font-size-18"><a href="{% url 'index' %}?year={{ date.year }}&month={{ date.month }}&day={{ date.day }}">{{ date.year }}年{{ date.month }}⽉{{ date.day }}⽇⽂章归档</a></span></p>
</li>
{% endfor %}
</ul>
</div>
<div class="links">
<h3>友情链接</h3>
<ul>
<li><a href="/">web开发</a></li>
<li><a href="/">前端设计</a></li>
<li><a href="/">Html</a></li>
<li><a href="/">CSS3</a></li>
<li><a href="/">Html5+css3</a></li>
<li><a href="/">百度</a></li>
</ul>
<div id="tbox"><a id="togbook" href="/"></a><a id="gotop" href="javascript:void(0)"></a></div>
</footer>
{% block myjs %}{% endblock %}
</body>
</html>
21、templates中index.html是⽤来显⽰主页的,它继承于base.html
{% extends 'base.html' %}
{% load staticfiles %}
{% block left %}
<div class="l_box f_l">
<div class="banner">
<div id="slide-holder">
<div id="slide-runner"><a href="/" target="_blank"><img id="slide-img-1" src='{% static 'images/a1.jpg' %}' alt=""/></a><a href="/" target="_blank"><img id="slide-img-2" src='{% static 'images/a2.jpg' %}' alt=""/></a><a href <div id="slide-controls">
<p id="slide-client" class="text"><strong></strong><span></span></p>
<p id="slide-desc" class="text"></p>
<p id="slide-nav"></p>
</div>
</div>
</div>
<script>
if(!window.slider) {
var slider={};
}
slider.data= [
{
"id":"slide-img-1", // 与slide-runner中的img标签id对应
"client":"标题1",
"desc":"这⾥修改描述这⾥修改描述这⾥修改描述" //这⾥修改描述
},
{
"id":"slide-img-2",
"client":"标题2",
"desc":"add your description here"
},
{
"id":"slide-img-3",
"client":"标题3",
"desc":"add your description here"
},
{
"id":"slide-img-4",
"client":"标题4",
"desc":"add your description here"
}
];
</script>
</div>
<!-- banner代码结束 -->
<div class="topnews">
<h2>最新⽂章</h2>
{% for article in pages %}
<div class="blogs">
<ul>
<h3><a href="{% url 'articles:article_detail' article.id %}">{{ article.title }}</a></h3>
<img src="{{ MEDIA_URL }}{{ article.image }}" width="600px" height="250px">
<p>{{ article.desc }}</p>
<p class="autor">
<span class="lm f_l"><a href="/">{{ }}</a></span>
<span class="dtime f_l">{{ article.add_time }}</span>
<span class="viewnum f_r">浏览(<a href="/">{{ article.click_num }}</a>)</span>
<span class="pingl f_r">评论(<a href="/">30</a>)</span>
</p>
</ul>
</div>
{% endfor %}
</div>
<div id="pagination">
<ul id="pagination-flickr">
{% if pages.has_previous %}
<li class="previous-off"><a href="{% url 'index' %}?pagenum={{ pages.previous_page_number}}&tagid={{ tagid }}&year={{ year }}&month={{ month }}&day={{ day }}">上⼀页</a></li>
{% endif %}
<li class="active">{{ pages.number }}/{{ pages.paginator.num_pages }}</li>
{% if pages.has_next %}
<li class="next"><a href="{% url 'index' %}?pagenum={{ pages.next_page_number}}&tagid={{ tagid }}&year={{ year }}&month={{ month }}&day={{ day }}">下⼀页&raquo;</a></li>
{% endif %}
</ul>
</div>
</div>
{% endblock %}
22、templates中login.html⽤于⽤户登录,是⼀个独⽴的模块:
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<meta charset="utf-8">
<link href="{% static 'css/reglogin.css' %}" rel='stylesheet' type='text/css' />
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="main">
<div class="header">
<h1>登录!</h1>
</div>
<p></p>
<form method="post" action="{% url 'users:user_login' %}">
{% csrf_token %}
<ul class="right-form">
<h2>登录:</h2>
<li><input type="text" placeholder="请输⼊你的⽤户名" name="username"/></li>
<li><input type="password" placeholder="请输⼊你的密码" name="password"/></li>
<input type="submit" value="登录">
<div class="clear"></div>
</ul>
<div class="clear"></div>
</form>
</div>。

相关文档
最新文档