一、變化的部分css
二、上代碼:html
{# 引用模板 #} {% extends 'base.html' %} {% load staticfiles %} {% block header_extends %} <link rel="stylesheet" href="{% static 'blog/blog.css' %}"> {% endblock %} {# 標題 #} {% block title %} {{ blog.title }} {% endblock %} {# 內容#} {% block content %} <div class="container"> <div class="row"> <div class="col-10 offset-1"> <ul class="blog-info-description"> <h3>{{ blog.title }}</h3> <li>做者:{{ blog.author }}</li> {# 時間過濾器讓時間按照本身須要的格式過濾 #} <li>發佈日期:{{ blog.created_time|date:"Y-m-d H:n:s" }}</li> <li>分類: <a href="{% url 'blogs_with_type' blog.blog_type.pk %}"> {{ blog.blog_type }} </a> </li> <li>閱讀({{ blog.get_read_num }})</li> </ul> <p class="blog-content">{{ blog.content|safe }}</p> <p>上一篇: {% if previous_blog %} <a href="{% url 'blog_detail' previous_blog.pk %}">{{ previous_blog.title }}</a> {% else %} <span>沒有了</span> {% endif %} </p> <p>下一篇: {% if next_blog %} <a href="{% url 'blog_detail' next_blog.pk %}">{{ next_blog.title }}</a> {% else %} <span>沒有了</span> {% endif %} </p> </div> </div> <div class="row"> <div class="col-10 offset-1"> <div class="comment-area"> <h3 class="comment-area-title">提交評論</h3> {% if user.is_authenticated %} <form action="{% url 'update_comment' %}" method="post" style="overflow: hidden"> {% csrf_token %} <div class="form-group"> <label for="form-control">{{ user.username }},歡迎評論~</label> <textarea class="form-control" name="text" id="comment_text" rows="4"></textarea> </div> <input type="hidden" name="object_id" value="{{ blog.pk }}"> <input type="hidden" name="content_type" value="blog"> <input type="submit" value="評論" class="btn btn-primary float-right"> </form> {% else %} 您還沒有登陸,登陸以後方可評論 {# 提交登陸的時候帶上從哪裏訪問的路徑 #} <a class="btn btn-primary" href="{% url 'login' %}?from={{ request.get_full_path }}">登陸</a> <span> or </span> <a class="btn-danger btn" href="{% url 'register' %}?from={{ request.get_full_path }}">註冊</a> {% endif %} </div> <div class="-comment-area"> <h3 class="comment-area-title">評論列表</h3> {% for comment in comments %} <div> {{ comment.user.username }} {{ comment.comment_time|date:"Y-m-d H:n:s" }} {{ comment.text }} </div> {% empty %} {% endfor %} </div> </div> </div> </div> {% endblock %} {% block js %} <script> $(".nav-blog").addClass("active").siblings().removeClass("active"); </script> {% endblock %}
# -*- coding: utf-8 -*-
# @Time : 18-11-20 下午8:10
# @Author : Felix Wang
from django import forms
from django.contrib import auth
from django.contrib.auth.models import User
class LoginForm(forms.Form):
username = forms.CharField(label='用戶名', required=True,
widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '請輸入用戶名'}))
# widget指定input標籤類型
password = forms.CharField(label='密碼',
widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': '請輸入密碼'}))
def clean(self): # 驗證數據
username = self.cleaned_data['username']
password = self.cleaned_data['password']
user = auth.authenticate(username=username, password=password)
if user is None:
raise forms.ValidationError('用戶名或密碼錯誤')
self.cleaned_data.user = user # 將驗證過的user放入clean_data
return self.cleaned_data
class RegisterForm(forms.Form):
# 用戶名字段
username = forms.CharField(label='用戶名',
max_length=30,
min_length=3,
required=True,
widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '請輸入用戶名'}))
# 郵箱字段
email = forms.EmailField(label='郵箱',
min_length=3,
required=True,
widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': '請輸入郵箱'}))
# 密碼字段
password = forms.CharField(label='密碼',
min_length=6,
required=True,
widget=forms.PasswordInput(
attrs={'class': 'form-control', 'placeholder': '請輸入密碼'}))
# 再次輸入密碼
password_again = forms.CharField(label='確認密碼',
min_length=6,
required=True,
widget=forms.PasswordInput(
attrs={'class': 'form-control', 'placeholder': '請再輸入一次密碼'}))
def clean_username(self):
username = self.cleaned_data['username']
if User.objects.filter(username=username).exists():
raise forms.ValidationError('用戶名已存在')
return username
def clean_email(self):
email = self.cleaned_data['email']
if User.objects.filter(email=email).exists():
raise forms.ValidationError('郵箱已存在')
return email
def clean_password_again(self):
password = self.cleaned_data['password']
password_again = self.cleaned_data['password_again']
if password != password_again:
raise forms.ValidationError('兩次輸入的密碼不一致')
return password_again
"""myblog URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path, include from django.conf import settings from django.conf.urls.static import static from . import views urlpatterns = [ path('', views.home, name='home'), # 主頁路徑 path('admin/', admin.site.urls), path('ckeditor', include('ckeditor_uploader.urls')), # 配置上傳url path('blog/', include('blog.urls')), # 博客app路徑 path('comment/', include('comment.urls')), # 博客app路徑 path('login/', views.login, name='login'), # 登陸 path('register/', views.register, name='register'), # 登陸 ] # 設置ckeditor的上傳 urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# -*- coding: utf-8 -*- # @Time : 18-11-7 下午4:12 # @Author : Felix Wang from django.shortcuts import render, redirect from django.contrib.contenttypes.models import ContentType from django.contrib import auth from django.contrib.auth.models import User from django.urls import reverse from read_statistics.utils import get_seven_days_read_data, get_x_days_hot_data from blog.models import Blog from .forms import LoginForm, RegisterForm def home(requests): blog_content_type = ContentType.objects.get_for_model(Blog) dates, read_nums = get_seven_days_read_data(blog_content_type) context = { 'read_nums': read_nums, 'dates': dates, 'today_hot_data': get_x_days_hot_data(0), # 獲取今日熱門 'yesterday_hot_data': get_x_days_hot_data(1), # 獲取昨日熱門 'seven_days_hot_data': get_x_days_hot_data(7), # 獲取周熱門 'one_month_hot_data': get_x_days_hot_data(30), # 獲取月熱門 } return render(requests, 'home.html', context) def login(requests): # 若是是form表單提交驗證登陸 if requests.method == 'POST': login_form = LoginForm(requests.POST) if login_form.is_valid(): # 驗證是否經過 # 由於在form表單驗證過了,因此不用本身再驗證 user = login_form.cleaned_data.get('user') auth.login(requests, user) return redirect(requests.GET.get('from', reverse('home'))) else: login_form.add_error(None, '用戶名或密碼不正確') else: login_form = LoginForm() context = { 'login_form': login_form, } return render(requests, 'login.html', context) def register(requests): if requests.method == 'POST': reg_form = RegisterForm(requests.POST) if reg_form.is_valid(): username = reg_form.cleaned_data['username'] email = reg_form.cleaned_data['email'] password = reg_form.cleaned_data['password'] # 建立用戶 user = User.objects.create_user(username=username, email=email, password=password) user.save() # 登陸用戶 user = auth.authenticate(username=username, password=password) auth.login(requests, user) # 登陸以後跳轉 return redirect(requests.GET.get('from', reverse('home'))) else: reg_form = RegisterForm() context = { 'reg_form': reg_form, } return render(requests, 'register.html', context)
{% extends 'base.html' %} {% load staticfiles %} {% block title %} 個人博客|登陸 {% endblock %} {% block content %} <div class="container"> <div class="col-xl-6 offset-xl-3"> <div class="card"> <h5 class="card-header">登陸</h5> <div class="card-body"> <form action="{% url 'login' %}" method="post"> {% csrf_token %} {% for field in login_form %} <label for="{{ field.id_for_label }}">{{ field.label }}</label> {{ field }} <p class="text-danger">{{ field.errors.as_text }}</p> {% endfor %} <span class="text-danger float-left">{{ login_form.non_field_errors }}</span> <input type="submit" value="登陸" class="btn btn-primary float-right"> </form> </div> </div> </div> </div> {% endblock %} {% block js %} {# 將首頁這個按鈕設置激活狀態 #} <script> $(".nav-home").addClass("active").siblings().removeClass("active"); </script> {% endblock %}
{% extends 'base.html' %} {% load staticfiles %} {% block title %} 個人博客|登陸 {% endblock %} {% block content %} <div class="container"> <div class="col-xl-6 offset-xl-3"> <div class="card"> <h5 class="card-header">登陸</h5> <div class="card-body"> <form action="{% url 'login' %}" method="post"> {% csrf_token %} {% for field in login_form %} <label for="{{ field.id_for_label }}">{{ field.label }}</label> {{ field }} <p class="text-danger">{{ field.errors.as_text }}</p> {% endfor %} <span class="text-danger float-left">{{ login_form.non_field_errors }}</span> <input type="submit" value="登陸" class="btn btn-primary float-right"> </form> </div> </div> </div> </div> {% endblock %} {% block js %} {# 將首頁這個按鈕設置激活狀態 #} <script> $(".nav-home").addClass("active").siblings().removeClass("active"); </script> {% endblock %}