• django模板加载静态资源


    1. 目录结构

    /mysite/setting.py部分配置:

    # Django settings for mysite project.
    import os.path
    
    TEMPLATE_DIRS = (
        # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
        # Always use forward slashes, even on Windows.
        # Don't forget to use absolute paths, not relative paths.
        # '/data/python/django/mysite/template'
        os.path.join(os.path.dirname(__file__), '../template').replace('\','/'),
      #这个例子使用了神奇的 Python 内部变量 __file__ ,该变量被自动设置为代码所在的 Python 模块文件名。 `` os.path.dirname(__file__)`` 将会获取自身所在的文件,即settings.py 所在的目录,然后由os.path.join 这个方法将这目录与 templates 进行连接。如果在windows下,它会智能地选择正确的后向斜杠”“进行连接,而不是前向斜杠”/”。 ) STATIC_PATH
    = os.path.join(os.path.dirname(__file__), '../media').replace('\','/')

    /mysite/urls.py配置:

    from django.conf.urls import patterns, include, url
    # from django.conf.urls import *
    # Uncomment the next two lines to enable the admin:
    from django.contrib import admin
    from page1.views import *
    from django.conf import *
    admin.autodiscover()
    
    urlpatterns = patterns('',
        # Examples:
        # url(r'^$', 'mysite.views.home', name='home'),
        # url(r'^mysite/', include('mysite.foo.urls')),
    
        # Uncomment the admin/doc line below to enable admin documentation:
        # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    
        # Uncomment the next line to enable the admin:
        # url(r'^admin/', include(admin.site.urls)),
        (r'^media/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_PATH}),
        
        url(r'^index/$',index),
        url(r'^monitor/$',monitor),
    )

    /mysite/views.py

    # Create your views here.
    # coding utf8
    from django.template import Template, Context
    # from django.http import HttpResponse
    from django.shortcuts import render_to_response
    
    def index(request):
        # return render_to_response('thanks.html')
        return render_to_response('index.html')

    /template/index.html

    <!DOCTYPE html>
    <html lang="zh-cn">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Bootstrap 101 Template</title>
    
        <!-- Bootstrap -->
        <link href="../media/css/bootstrap.min.css" rel="stylesheet">
    
        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
          <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
          <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
      </head>
      <body>
        <h1>你好,世界!</h1>
    
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="../media/js/bootstrap.min.js"></script>
      </body>
    </html>

     成功加载资源:

     

    备注:上面的index.html用到了bootstrap,参考地址:http://www.bootcss.com/

  • 相关阅读:
    2021.8.2—2021.8.8
    2021.7.29
    2021.7.27
    2021.7.26
    GC日志分析
    学习类加载机制笔记
    4、SpringCloud停更说明
    3、SpringCloudAlibaba版本选择
    京东、阿里的微服务架构
    nacos集群安装
  • 原文地址:https://www.cnblogs.com/forilen/p/4117479.html
Copyright © 2020-2023  润新知