- login.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<title>登陆</title>
</head>
<body style="background-image: url({%static 'img/backimg.gif' %});">
<div style=" 70%; margin: 0 auto; padding-top: 20em;padding-bottom: 20em;">
<div class="ui stackable centered column grid">
<div class="centered row">
<div class="six wide column">
<div style="padding-bottom: 5em; background-color: rgba(245, 245, 245,0.5);" class="ui segment">
<form action="" method="POST" class="ui form">
{% csrf_token %}
<h3 style="color: red;">{{ msg }}</h3>
<div class="field">
<label for="username">用户名:</label>
<input type="text" name="username" id="">
</div>
<div class="field">
<label for="password">密码:</label>
<input type="password" name="password" id="">
</div>
<input style="float: right;" class="ui inverted green button" type="submit" value="登陆">
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
- regist.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<title>注册</title>
</head>
<body style="background-image: url({% static 'img/backimg.gif' %});">
<div style=" 70%; margin: 0 auto; padding-top: 20em;padding-bottom: 20em;">
<div class="ui stackable centered column grid">
<div class="centered row">
<div class="six wide column">
<div style="padding-bottom: 5em; background-color: rgba(245, 245, 245,0.5);" class="ui segment">
<form action="" method="POST" class="ui form">
{% csrf_token %}
<h3 style="color: red;">{{ msg }}</h3>
<div class="field">
<label for="username">用户名:</label>
<input type="text" name="username" id="">
</div>
<div class="field">
<label for="password">密码:</label>
<input type="password" name="password" id="">
</div>
<div class="field">
<label for="password2">重复密码:</label>
<input type="password" name="password2" id="">
</div>
<div class="field">
<label for="email">Email:</label>
<input type="text" name="email" id="">
</div>
<input style="float: right;" class="ui inverted green button" type="submit" value="注册">
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
- /app/urls.py
from django.contrib import admin
from django.urls import path,re_path
from blog import views
urlpatterns = [
path('',views.index),
path('index',views.index),
re_path('article/(d+)',views.article),
path('login/',views.logins),
path('regist/',views.regist),
path('logout/',views.log_out),
]
- index.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<title>首页</title>
</head>
<body style="background-color: rgb(245, 245, 245);">
<div style="min-height: 70px;" class="ui inverted fixed stackable red large menu">
<a class="item" href="/blog">My Blog</a>
{% if request.user.is_active %}
<div class="right menu">
<a class='item'>{{ request.user.username }}</a>
<a class='item' href="logout/">退出</a>
</div>
{% else %}
<div class="right menu"></div>
<a class="item" href="/blog/regist">注册</a>
<a class="item" href="/blog/login">登陆</a>
</div>
{% endif %}
</div>
<div style=" 70%;margin: auto;padding-top: 15em;">
<div class="ui four stackable cards">
{% for ar in articles_list %}
<div class="ui card">
<div class="image">
<!--img src="{% static '{{ ar.image }}'%}" 这里不要static-->
<img src="/media/{{ ar.image }}">
</div>
<div class="content">
<a class="header" href="article/{{ ar.id }}">{{ ar.title }}</a>
<div class="meta">
</div>
<div class="description">
<div class="extra content">
<span class="right floated"> {{ ar.create_date }} </span>
<span>
<i class="user icon"></i>
admin
</span>
</div>
</div>
</div>
</div>
{% endfor %}
<div class="ui card">
<div class="image">
<img src="{% static 'img/salad.jpg'%}">
</div>
<div class="content">
<a class="header">可爱的小狗</a>
<div class="meta">
</div>
<div class="description">
<div class="extra content">
<span class="right floated">2020年6月29日</span>
<span>
<i class="user icon"></i>
admin
</span>
</div>
</div>
</div>
</div>
<div class="ui card">
<div class="image">
<img src="{% static 'img/salad.jpg'%}">
</div>
<div class="content">
<a class="header">可爱的小狗</a>
<div class="meta">
</div>
<div class="description">
<div class="extra content">
<span class="right floated">2020年6月29日</span>
<span>
<i class="user icon"></i>
admin
</span>
</div>
</div>
</div>
</div>
<div class="ui card">
<div class="image">
<img src="{% static 'img/salad.jpg'%}">
</div>
<div class="content">
<a class="header">可爱的小狗</a>
<div class="meta">
</div>
<div class="description">
<div class="extra content">
<span class="right floated">2020年6月29日</span>
<span>
<i class="user icon"></i>
admin
</span>
</div>
</div>
</div>
</div>
<div class="ui card">
<div class="image">
<img src="img/salad.jpg">
</div>
<div class="content">
<a class="header">可爱的小狗</a>
<div class="meta">
</div>
<div class="description">
<div class="extra content">
<span class="right floated">2020年6月29日</span>
<span>
<i class="user icon"></i>
admin
</span>
</div>
</div>
</div>
</div>
</div>
<div style="padding: 30px 0 30px 0;" class="ui vertical segment">
<div class="ui centered grid">
<div class="ui pagination menu">
<a class="item">
<i class="icon left arrow"></i>
</a>
<a class="item" href="#">
1
</a>
<a class="item" href="#">
2
</a>
<a class="item" href="#">
....
</a>
<a class="item">
<i class="icon right arrow"></i>
</a>
</div>
</div>
</div>
</div>
</body>
</html>
- article.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<title>文章</title>
</head>
<style>
img{
100%;
}
</style>
<body style="background-color: rgb(245, 245, 245);">
<div style="min-height: 70px;" class="ui inverted fixed stackable red large menu">
<a class="item" href="/blog">My Blog</a>
{% if request.user.is_active %}
<div class="right menu">
<a class='item'>{{ request.user.username }}</a>
<a class='item' href="/blog/logout/">退出</a>
</div>
{% else %}
<div class="right menu">
<a class="item" href="/blog/regist/">注册</a>
<a class="item" href="/blog/login/">登陆</a>
</div>
{% endif %}
</div>
<div style=" 90%;margin: auto;padding: 15em 0 5em 0;">
<div style="min-height: 600px;" class="ui segment">
<div class="header" style="text-align: center;"><h3> {{ artc.title }}</h3></div>
<img src="{% static 'img/timg (1).jpg'%}" alt="">
<p> {{ artc.content }}</p>
</div>
</div>
</body>
</html>
- views.py
from django.shortcuts import render,redirect
from blog import models
from django.contrib.auth.models import User
from django.contrib.auth import authenticate,login,logout
# Create your views here.
def index(request):
articles_list = models.article.objects.all()
return render(request,'blog/index.html',locals())
def article(request,id):
artc = models.article.objects.get(id=id)
return render(request,'blog/article.html',locals())
def logins(request):
if request.method == 'POST':
username = request.POST.get("username")
password = request.POST.get("password")
user = authenticate(username=username,password=password)
if user:
login(request,user)
return redirect('/blog/index')
else:
msg = '用户名或密码错误!'
return render(request,'blog/login.html',locals())
return render(request,'blog/login.html')
def regist(request):
if request.method == 'POST':
username = request.POST.get("username")
password = request.POST.get("password")
password2 = request.POST.get("password2")
email = request.POST.get("email")
if password != password2:
msg = '两次输入的密码不一样!'
return render(request,'blog/regist.html',locals())
elif username == '':
msg = '用户名不能为空'
return render(request,'blog/regist.html',locals())
cusor = User.objects.create_user(username=username,password=password,email=email)
cusor.save()
return redirect('/blog/login')
return render(request,'blog/regist.html')
def log_out(request):
logout(request)
return redirect('/blog/')
pro/urls.py
"""test1 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/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 test1 import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# path('',include('booktest.urls')),
path('blog/',include('blog.urls')),
]
urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)