• 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()),
    )
  • 相关阅读:
    Aptana 由于没有关闭编辑器而导致的启动不起来了。
    postgresql备份导入数据库小记
    [转] js 事件冒泡 阻止
    ruby 取得ip
    [ 转 ] 网页聊天室的原理
    ryby 数组笔记
    第一个rails应用
    vue-router-5-命名路由
    vue-router-4-编程式导航
    vue-router-3-嵌套路由
  • 原文地址:https://www.cnblogs.com/tangpg/p/10825688.html
Copyright © 2020-2023  润新知