Django and Python build personal blogs and websites

Posted by audiomove on Sun, 17 Nov 2019 15:17:34 +0100

Build a personal blog

Preface:

Why do I blog?
Why did I start blogging? I'm not a big fan

This article tells me, not only me, but also the new rookie like me
To write a blog and build a website is not to show off knowledge, or to entertain, but to summarize their knowledge, improve the structure system, and express their feelings


Get to the point


1. Directory structure


The most important module is the article module of ArticleApp
Then there are user app modules about users, such as login, registration, comment Association, message, etc
Second, OtherApp and PersonalSpaceApp include personal news, announcements, broadcast messages, friend chains, etc
Other modules originally wanted to be more perfect, but for various reasons, let's see if we have time in the future. Originally, we wanted to build a hierarchical architecture, similar to mvc. Think of a small blog website, and then we gave up because of our lack of ability


2. Table structure

First map

Core module
Left to right, top to bottom

  1. User table
  2. Comment table: faher_comment_id is a self connection, which indicates whether it is a comment or a reply. If there is a comment, it indicates whether it is a reply
  3. Classification table
  4. Article table
  5. Tag article many to many middle table
  6. Tab table
  7. Thematic table

Edge module

  1. Friend list
  2. Personal dynamic table
  3. Announcement form
  4. User table
  5. guestbook
  6. Unfamiliar user table: automatically established according to ip, used to count the number of visitors and the number of visitors

3. Visit statistics

import uuid

from django.db.models import F

from UserApp.models import StrangeUser, UserAccount

USER_KEY = 'uid'


class UserAccessMiddleWare:
    def __init__(self,get_response):
        self.get_response = get_response

    def __call__(self,request):

        email = request.session.get('user_email')
        if email:       //Increase access if the user is already logged in
            UserAccount.objects.filter(email=email).update(access_count = F('access_count')+1)
            request.user_account = UserAccount.objects.filter(email=email)[0]
        else:
            request.user_account = None


        sUser = self.get_sUser(request)
        request.sUser = sUser

        response = self.get_response(request)
        return response

    def get_sUser(self,request)://Obtain the ip address of unknown user and store it in the library
        uid = request.session.get(USER_KEY)
        if uid!=None:
            sUser = StrangeUser.objects.filter(uid=uid)
            sUser.update(access_count=F('access_count') + 1)
            return sUser[0]
        else:
            uid = request.META.get('REMOTE_ADDR')
            request.session.set_expiry(60*60*24*10)
            request.session[USER_KEY] = uid
            sUser = StrangeUser.objects.filter(uid=uid)

            if len(sUser):  //Stock in if new user
                sUser.update(access_count=F('access_count')+1)
            else:
                sUser=[]
                ssUser = StrangeUser()
                ssUser.uid = uid
                ssUser.access_count = 1
                ssUser.save()
                sUser.append(ssUser)
            return sUser[0]




4. Project screenshot






Demo address: http://www.binnb.top
github address: https://github.com/biningo/Biningo-Blog
Welcome star!

Topics: Python Session Django github