start django project

1.django-admin startproject helloword 创建项目helloword

2.开始一个app,写一个hello world
python manage.py startapp hello

4.settings db

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',                    # 你的数据库引擎
        'HOST': "localhost",                                     # 你的数据地址,localhost代表本地
        "PORT": 3306,                                            # 端口, 数据库的默认端口一般是3306
        "USER": "root",                                         # 用户名
        "PASSWORD": "123456",                                      # 密码
        "NAME": "study"                                          # 库名
    }
}

# CACHES = {
#     "default":{
#         "BACKEND":"django_redis.cache.RedisCache",
#         "LOCATION":"redis://127.0.0.1:6379/",
#         "OPTIONS":{
#             "CLIENT_CLASS":"django_redis.client.DefaultClient"
#         }
#     }
# }

4.view

import json
from django.shortcuts import render, redirect, reverse
from django.http import HttpResponse
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages  # 错误提示信息
from django.views.decorators.csrf import csrf_exempt
from io import BytesIO
from django.views import View
from django.forms.utils import ErrorDict
from django.core.cache import cache
from show.models import Book
# Create your views here.
class CacheVisit(View):
    """
    访问数据库缓存
    from django.core.cache import cache
    """
    def get(self, request):
        books = Book.objects.all()
        return render(request, '1.html', locals())

5. 1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body>
    {% for book in books %}
        {{ book.title }}&nbsp;&nbsp;&nbsp;
        {{ book.author }}&nbsp;&nbsp;&nbsp;
        {{ book.download_text }}&nbsp;&nbsp;&nbsp;
        {{ book.new }}<br>
    {% endfor %}
</body>
</html>

6.url路由

from django.conf.urls import url
from django.contrib import admin
from show import views as view

urlpatterns =( 
url(r'^admin/', admin.site.urls),
url(r'^show/',view.CacheVisit.as_view()),
)
原文地址:https://www.cnblogs.com/tangpg/p/10825688.html