搭建python项目
使用pycharm编辑器直接创建django项目 项目名:MyfirstApp
配置网站路由
在项目名称同名的文件夹中配置路由
url(r"^myapp/",include('MyFirstApp.urls')),#路由分发到实际app中的urls
MyFirstApp文件夹下urls.py的配置如下:
from django.contrib import admin
from django.urls import path
from MyFirstApp import views
from django.conf import settings
from django.conf.urls import url,include
from django.views.generic.base import TemplateView
from django.conf.urls.static import static
urlpatterns = [
url(r'login$', views.myapplogin), # 登陆
url(r'register$', views.myappregister), # 注册
url(r'index$', views.myappindex), # 首页
url(r'writeblog$',views.WriteBlog),#写博客
url(r'article$', views.myappothers), # 知识宝库
url(r'article_detail', views.myappArticleDetail), # 明细
url(r'Amazinglife', views.myappothers), # 创意生活
url(r'funnytime', views.myappothers), # 轻松一刻
url(r'mood', views.myappothers), # 时光脚步
url(r'board', views.myappothers), # 留言板
url(r'about', views.myappothers), # 关于
url(r'error',views.myappErrorShow), #错误提示
url(r'404error',views.error404)
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) #这句话是用来指定和映射静态文件的路径
首先看看整体网站的页面情况:如下图
网站首页
知识宝库
创意生活
轻松一刻
.
时光脚步
留言板
团队介绍
写博客
登录注册
举例说明视图部分
使用django优越的session功能记录用户信息使之登录后才能编写博客。
from django.shortcuts import render, redirect
from MyFirstApp import models
from django.db.models import Q
from django.http import HttpResponse
from datetime import datetime, time
# Create your views here.
def myapplogin(request): # 登陆
if request.method == 'POST':
username = request.POST.get('username')
# 判断用户名 密码是否合法用户
password = request.POST.get('password')
users = models.userinfo.objects.filter(Q(user_Name=username) | Q(email=username))
if len(users) <= 0:
return redirect("/myapp/error?ErrorMessage=" + "用户名不存在")
else:
# 判断密码正确不
if users[0].password == password:
# 写入session 存入服务器
request.session['username'] = username;
return redirect("/myapp/index")
else:
return redirect("/myapp/error?ErrorMessage=" + "密码错误!")
else:
return render(request, "login.html")
创建博客数据库表
from django.db import models
from django.utils import timezone
# Create your models here.
class userinfo(models.Model):#用户注册信息
user_Name = models.CharField(max_length=64)
email = models.CharField(max_length=128)
phone = models.CharField(max_length=64)
password = models.CharField(max_length=64,default='123456')
class myselfinfo(models.Model):#作者个人信息
name = models.CharField(max_length=64)
email = models.CharField(max_length=128)
phone = models.CharField(max_length=64)
memo=models.TextField(max_length=1024)
class blogMainRecord(models.Model):#博客主记录
blogserialno=models.CharField(verbose_name='博客唯一标识符',max_length=16)
blogtype=models.CharField(verbose_name='博客类别',max_length=16)
blogtag=models.CharField(verbose_name='博客标签',max_length=64)
blogtitle = models.CharField(verbose_name='博客标题', max_length=128)
blogabstract = models.CharField(verbose_name='博客摘要', max_length=128)
blogtitle_picsrc= models.CharField(verbose_name='博客图片路径', max_length=128,default="/static/temp/art.jpg")
blogclick_nums = models.IntegerField(verbose_name='点击量', default=0)
bloghot_nums = models.IntegerField(verbose_name='热度', default=0)
blogcreator=models.CharField(verbose_name='博客创建人',max_length=16)
blogcreate_time = models.DateTimeField(verbose_name='创建时间', default=timezone.now)
class IMG(models.Model):#博客封面图
blogserialno = models.CharField(verbose_name='博客唯一标识符', max_length=16)
img = models.ImageField(upload_to='img')
name = models.CharField(max_length=20)
class blogDetailRecord(models.Model):#博客内容明细
blogserialno = models.CharField(verbose_name='博客唯一标识符', max_length=16)
blogcontent = models.TextField(verbose_name='正文', default='')
模板文件主要介绍
界面抬头固定菜单栏写入 headmain.html文件
代码:
<div class="navbar navbar-fixed-top">
<div class="container cl">
<a class="navbar-logo hidden-xs" href="index.html">
<img class="logo" src="/static/img/logo.png" alt="小乌龟和鬼博客"/>
</a>
<a class="logo navbar-logo-m visible-xs" href="index.html">小乌龟和鬼博客</a>
<a aria-hidden="false" class="nav-toggle Hui-iconfont visible-xs" href="javascript:void(0);"
onclick="showSide();"></a>
<nav class="nav navbar-nav nav-collapse w_menu" role="navigation">
<ul class="cl">
<li class="active"><a href="/myapp/index" data-hover="首页">首页</a></li>
<li><a href="/myapp/article" data-hover="知识宝库">知识宝库</a></li>
<li><a href="/myapp/Amazinglife" data-hover="创意生活">创意生活</a></li>
<li><a href="/myapp/funnytime" data-hover="轻松一刻">轻松一刻</a></li>
<li><a href="/myapp/mood" data-hover="时光脚步">时光脚步</a></li>
<li><a href="/myapp/board" data-hover="留言板">留言板</a></li>
<li><a href="/myapp/about" data-hover="团队介绍">团队介绍</a></li>
</ul>
</nav>
<nav class="navbar-nav navbar-userbar hidden-xs hidden-sm" style="top: 0;">
<ul class="cl">
{% if user_name %}
<li class="userInfo dropDown dropDown_hover">
<a href="javascript:;">
<img class="avatar radius" src="/static/img/qq.jpg" alt="小乌龟和鬼"> {{ user_name }}</a>
<ul class="dropDown-menu menu radius box-shadow">
<li><a href="/myapp/writeblog">写博客</a></li>
<li><a href="/myapp/loginOut">个人信息</a></li>
<li><a href="/myapp/loginOut">收藏</a></li>
<li><a href="/myapp/loginOut">注销</a></li>
</ul>
</li>
{% else %}
<li class="userInfo dropDown dropDown_hover">
<a href="/myapp/login" ><img
class="avatar size-S" src="/static/img/qq.jpg" title="登录">登录</a>
</li>
{% endif %}
</ul>
</nav>
</div>
<script></script>
</div>
每个界面调用方法
<header class="navbar-wrapper">
{% include "headmain.html" %}
</header>
把常用的js以及css放入模板中
commontjscss.html:
<link type="text/css" rel="stylesheet" href="/static/css/bootstrap.css"/>
<script type="text/javascript" src="/static/js/vue.js"></script>
<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/js/vue-resource.js"></script>
<script type="text/javascript" src="/static/js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="/static/plugin/h-ui/css/H-ui.min.css"/>
<link rel="stylesheet" type="text/css" href="/static/plugin/Hui-iconfont/1.0.8/iconfont.min.css"/>
<link rel="stylesheet" type="text/css" href="/static/css/common.css"/>
<link rel="stylesheet" type="text/css" href="/static/plugin/pifu/pifu.css"/>
页面细节功能还在持续完善中。
本项目中使用了好多js炫酷特效,数据交互以及vue的使用尝试用了多种方式。适合新手学习使用。
喜欢的朋友可以一起研究下哈。
记得点赞哟
源码:百度网盘链接:https://pan.baidu.com/s/1JYSjWPibzZCo7LlqrDzOsA?pwd=ohmc
提取码:ohmc