start django project

1.django-admin startproject helloword 建立項目hellowordhtml

2.開始一個app,寫一個hello world
python manage.py startapp hellopython

4.settings dbmysql

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.viewredis

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.htmlsql

<!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()),
)
相關文章
相關標籤/搜索