• python Django的个人博客


    前言

    本项目主要依赖于Django框架,搭建一个属于自己的个人博客系统,分为登录界面和若干个展示界面,主要用到python,mysql。

    环境配置

    Python : 3.6.8
    
    Django: 3.2.13
    
    Mysqlclient :1.4.6
    
    navicat : navicat111_premium_cs_x64
    
    Pycharm: JetBrains PyCharm 2018.3.5 x64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    博客介绍

    采用可爱的豆包猫咪为主角,依赖django框架创建豆包的个人博客。
    我家豆包

    实现步骤

    首先,是创建django项目

    python manage.py startapp blogs
    
    • 1

    核心代码:

    Model.py

    1.	from django.db import models  
    2.	  
    3.	# Create your models here.  
    4.	class User(models.Model):  
    5.	    sname = models.CharField(max_length=50, default='')  
    6.	    spassword = models.CharField(max_length=50, default='')  
    7.	  
    8.	    def __str__(self):  
    9.	        return self.sname  
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    view.py

    1.	import mimetypes  
    2.	import os  
    3.	import re  
    4.	from wsgiref.util import FileWrapper  
    5.	  
    6.	from django.conf import settings  
    7.	from django.http import StreamingHttpResponse  
    8.	from django.shortcuts import render  
    9.	from blogsapp.models import User  
    10.	# Create your views here.  
    11.	def login(request):  
    12.	    if request.method == 'POST':  
    13.	        user_name  = request.POST.get('username', '')  
    14.	        pass_word = request.POST.get('password', '')  
    15.	        user = User.objects.filter(sname = user_name)  
    16.	        if user:  
    17.	            user = User.objects.get(sname = user_name)  
    18.	            if pass_word == user.spassword:  
    19.	                return render(request, 'index.html', {'user': user})  
    20.	            else:  
    21.	                return render(request, 'login.html', {'error': '密码错误'})  
    22.	        else:  
    23.	            return render(request, 'login.html', {'error': '用户名不存在'})  
    24.	    else:  
    25.	        return render(request, 'login.html')  
    26.	  
    27.	def logsuccess(request):  
    28.	    return render(request, 'index.html')  
    29.	  
    30.	def photography(request):  
    31.	    return render(request, 'photography.html') 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    bolgs/urls.py

    16.	from django.contrib import admin  
    17.	from django.urls import path  
    18.	from django.urls import include  
    19.	from blogsapp import views  
    20.	urlpatterns = [  
    21.	    path('admin/', admin.site.urls),  
    22.	    path('blogsapp/', include('blogsapp.urls')),  
    23.	    path('', views.login, name='login'),  
    24.	    path('index', views.logsuccess, name='index'),  
    25.	    path('photography', views.photography, name='photography'),  
    26.	    path('travel', views.travel, name='travel')  
    27.	] 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    Blogsapp/url.py

    1.	from django.test import TestCase  
    2.	  
    3.	# Create your tests here.  
    4.	from django.contrib import admin  
    5.	from django.urls import path  
    6.	from django.urls import include  
    7.	from blogsapp import views  
    8.	app_name = 'blogsapp'  
    9.	  
    10.	urlpatterns = [  
    11.	    path('login/', views.login, name='login'),  
    12.	    path('index/', views.logsuccess, name='logsuccess'),  
    13.	    path('', views.photography, name='photography'),  
    14.	    path('', views.travel, name='travel')  
    15.	]  
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Setting.py

    1.	#连接数据库  
    2.	DATABASES = {  
    3.	    'default': {  
    4.	        # 'ENGINE': 'django.db.backends.sqlite3',  
    5.	        # 'NAME': BASE_DIR / 'db.sqlite3',  
    6.	        'ENGINE' : 'django.db.backends.mysql',  
    7.	        'NAME' : 'mydata9th_1986',  
    8.	        'USER' :  'root',  
    9.	        'PASSWORD' : 'admin123',  
    10.	        'HOST' : '127.0.0.1',  
    11.	        'PORT' : '3306'  
    12.	    }  
    13.	}  
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    运行截图

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    总结:

    在html中,利用模板文件,在其基础上修改,一定要添加{% load static %},只有通过此变量才能访问到static文件夹内的静态文件与图片
    在页面跳转时,应修改为<a href="{%url 'photography' %}">,通过此种跳转方法,才能访问到正确的页面。
    在项目部署的过程中,首先需要修改setting,添加数据库 app等信息,然后在models views urls 添加函数,生成数据库表与访问路由/前端交互,再增加html文件,达到一个访问的流程。

    代码获取

    评论留言个人邮箱,赞赏50,收到会回复
    请添加图片描述

  • 相关阅读:
    计算机视觉的优势和挑战
    xargs命令的基本用法
    git常用命令
    Swift 网络请求 Moya+RxSwift
    PHP 严格模式
    用于演示文稿的新 Dapr 幻灯片
    移动端开发:WebView介绍和使用、JSBridge等
    ARM 自己动手安装交叉编译工具链
    00后最关注程序员,超8成人接受灵活就业,视频UP主是最想从事的职业
    THP Maleimide,1314929-99-1,THP-Mal凯新生物双功能螯合剂
  • 原文地址:https://blog.csdn.net/Dummy_/article/details/125601069