From 30d66bffb16fbf8d900eef2a51753d7c8d21489f Mon Sep 17 00:00:00 2001 From: chaos <381810956@qq.com> Date: Sun, 11 Apr 2021 20:23:08 +0800 Subject: [PATCH 1/4] =?UTF-8?q?Create=20=E7=AC=AC=E5=8D=81=E4=BA=94?= =?UTF-8?q?=E5=91=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\254\254\345\215\201\344\272\224\345\221\250/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/.keep" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/.keep" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/.keep" new file mode 100644 index 00000000..e69de29b -- Gitee From cbb4d19e982837500001d55292fc735cb206524a Mon Sep 17 00:00:00 2001 From: chaos <381810956@qq.com> Date: Sun, 11 Apr 2021 20:23:49 +0800 Subject: [PATCH 2/4] django view --- .../Django\350\247\206\345\233\276.md" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/Django\350\247\206\345\233\276.md" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/Django\350\247\206\345\233\276.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/Django\350\247\206\345\233\276.md" new file mode 100644 index 00000000..1c831e0a --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/Django\350\247\206\345\233\276.md" @@ -0,0 +1,87 @@ +## URL 配置 + +自上向下查找 `urlpatterns` + +- urlpatterns + + ``` + urlpatterns = [ + path('polls/', include("polls.urls")), + path('admin/', admin.site.urls), + ] + ``` + +- 变量匹配 + + ``` + path('/vote/', views.vote, name='vote'), + ``` + +- 正则匹配 + + ``` + import re + + path = "5/vote/" + m = re.math(r'^(?P\d+)/vote/$', path) + print(m.group("question_id")) + ``` + +## 视图 + +- 基于函数的视图 + + ```python + def index(request): + latest_question_list = Question.objects.order_by('-pub_date')[:5] + context = { + 'latest_question_list': latest_question_list, + } + return render(request, 'polls/index.html', context) + + path('', views.index, name='index'), + + def index_get(request): + pass + + def index_post(request): + pass + + def index_put(request): + pass + + def index_delete(request): + pass + ``` + +- 基于类的视图 + + ```python + class IndexView(generic.TemplateView): + template_name = "polls/index.html" + + def get_context_data(self, **kwargs): + latest_question_list = Question.objects.order_by('-pub_date')[:5] + context = { + 'latest_question_list': latest_question_list, + } + return context + + path('', views.IndexView.as_view(), name='index'), + + class IndexView(generic.View): + def get(self, *args, **kwargs): + pass + + def post(self, *args, **kwargs): + pass + + def put(self, *args, **kwargs): + pass + + def delete(self, *args, **kwargs): + pass + ``` + + + -- Gitee From bdf5f29adc32ac7cbf51197cbe5cce7daf9f89aa Mon Sep 17 00:00:00 2001 From: chaos <381810956@qq.com> Date: Sun, 11 Apr 2021 20:25:11 +0800 Subject: [PATCH 3/4] project structure --- ...36\346\210\230\351\241\271\347\233\256.md" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/\347\254\254\344\270\200\344\270\252Django\345\256\236\346\210\230\351\241\271\347\233\256.md" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/\347\254\254\344\270\200\344\270\252Django\345\256\236\346\210\230\351\241\271\347\233\256.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/\347\254\254\344\270\200\344\270\252Django\345\256\236\346\210\230\351\241\271\347\233\256.md" new file mode 100644 index 00000000..48597c0f --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/\347\254\254\344\270\200\344\270\252Django\345\256\236\346\210\230\351\241\271\347\233\256.md" @@ -0,0 +1,41 @@ + + +## 第一个Django实战项目 + +``` +$ tree -I __pycache__ +. +└── myblog + ├── blog + │   ├── __init__.py + │   ├── admin.py + │   ├── apps.py + │   ├── migrations + │   │   ├── 0001_initial.py + │   │   ├── 0002_category_tag.py + │   │   └── __init__.py + │   ├── models.py + │   ├── tests.py + │   └── views.py + ├── manage.py + ├── myblog + │   ├── __init__.py + │   ├── settings.py + │   ├── urls.py + │   └── wsgi.py + ├── statics + │   ├── css + │   │   └── style.css + │   ├── images + │   │   └── avatar.jpg + │   └── js + │   ├── application.js + │   ├── insight.js + │   └── plugin.min.js + └── templates + ├── details.html + └── index.html +``` + + + -- Gitee From 57bcb56914ecb1062f6b87dbdaca309e749f2722 Mon Sep 17 00:00:00 2001 From: chaos <381810956@qq.com> Date: Sun, 11 Apr 2021 20:28:17 +0800 Subject: [PATCH 4/4] the most important file in this practice --- .../admin.py" | 17 +++ .../apps.py" | 5 + .../models.py" | 31 +++++ .../settings.py" | 129 ++++++++++++++++++ .../urls.py" | 27 ++++ .../views.py" | 43 ++++++ 6 files changed, 252 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/admin.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/apps.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/models.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/settings.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/urls.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/views.py" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/admin.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/admin.py" new file mode 100644 index 00000000..9f1203d5 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/admin.py" @@ -0,0 +1,17 @@ +from django.contrib import admin +from .models import Article +from .models import Category,Tag + + +class CategoryInline(admin.TabularInline): + model = Category + +class TagInline(admin.TabularInline): + model = Tag + +class ArticleAdmin(admin.ModelAdmin): + inlines = [CategoryInline, TagInline] + list_display = ['title', 'create_time'] + +# Register your models here. +admin.site.register(Article, ArticleAdmin) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/apps.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/apps.py" new file mode 100644 index 00000000..225ff244 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/apps.py" @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class BlogConfig(AppConfig): + name = 'blog' diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/models.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/models.py" new file mode 100644 index 00000000..099e503c --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/models.py" @@ -0,0 +1,31 @@ +from django.db import models +from mdeditor.fields import MDTextField + + +# Create your models here. +class Article(models.Model): + title = models.CharField(max_length=200) + text = MDTextField() + url = models.CharField(max_length=200) + update_time = models.DateTimeField(auto_now=True) + create_time = models.DateTimeField(auto_now_add=True) + + class Meta: + db_table = "article" + + def __str__(self): + return f"
" + + +class Category(models.Model): + article = models.ForeignKey(Article, on_delete=models.CASCADE) + name = models.CharField(max_length=200) + slug = models.CharField(max_length=200) + uri = models.CharField(max_length=200) + + +class Tag(models.Model): + article = models.ForeignKey(Article, on_delete=models.CASCADE) + name = models.CharField(max_length=200) + slug = models.CharField(max_length=200) + uri = models.CharField(max_length=200) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/settings.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/settings.py" new file mode 100644 index 00000000..ec704c79 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/settings.py" @@ -0,0 +1,129 @@ +""" +Django settings for myblog project. + +Generated by 'django-admin startproject' using Django 2.0. + +For more information on this file, see +https://docs.djangoproject.com/en/2.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/2.0/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'ivtcw1tqw%ob&l$=j$i)t=t99^5enpy=d@kkx$40v5khv+ch+2' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'mdeditor', + 'blog.apps.BlogConfig', + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'myblog.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [os.path.join(BASE_DIR, "templates")], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'myblog.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/2.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': 'blogs', + 'USER': 'root', + 'PASSWORD': '123456789', + 'HOST': '127.0.0.1', + 'PORT': '3306', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/2.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/2.0/howto/static-files/ + +STATIC_URL = '/statics/' +STATICFILES_DIRS = [ + os.path.join(BASE_DIR, "statics"), +] diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/urls.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/urls.py" new file mode 100644 index 00000000..60a61ad7 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/urls.py" @@ -0,0 +1,27 @@ +"""myblog URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/2.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 +from django.conf.urls.static import static +from django.conf import settings +from blog.views import IndexView, DetailView + + +urlpatterns = [ + path('admin/', admin.site.urls), + path("", IndexView.as_view()), + path("///", DetailView.as_view()), +] + static(settings.STATIC_URL) diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/views.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/views.py" new file mode 100644 index 00000000..f2e45835 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_chaos/\347\254\254\345\215\201\344\272\224\345\221\250/views.py" @@ -0,0 +1,43 @@ +from django.shortcuts import render +from django.views.generic import TemplateView +from .models import Article +import math +import markdown + + +# Create your views here. +class IndexView(TemplateView): + template_name = "index.html" + + def get(self, request, *args, **kwargs): + article_list = Article.objects.order_by("-create_time") + for article in article_list: + article.pub_date = article.create_time.strftime("%m-%d") + article.length = len(article.text) + article.read_time = math.ceil(len(article.text)/180) if article.text else 0 + article.categories = article.category_set.values() + article.tags = article.tag_set.values() + + context = { + "article_list": article_list, + } + return self.render_to_response(context) + +class DetailView(TemplateView): + template_name = "detail.html" + + def get(self, request, *args, **kwargs): + article = Article.objects.get(url=request.path) + content = "" + for line in article.text.split("\n"): + content += line.strip(" ") if "```" in line else line + content += "\n" + article.content = markdown.markdown(content, extensions=[ + 'markdown.extensions.extra', + 'markdown.extensions.codehilite', + 'markdown.extensions.toc', + ]) + context = { + "article": article, + } + return self.render_to_response(context) \ No newline at end of file -- Gitee