Python代码片段之Django静态文件URL的配置

这篇具有很好参考价值的文章主要介绍了Python代码片段之Django静态文件URL的配置。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

首先要说明这段python代码并不完整,而且我也没有做过测试,只是我在工作时参考了其中的一些个方法。这是我在找python相关源码资料里看到的一段代码,是Django静态文件URL配置代码片段2,代码中有些方法还是挺技巧的,做其它操作时可以参考着使用。需要完整代码的伙伴们可以自已去找找看,如果有时间等,就等我把其它片段收集整理后再贴上来分享。

#!usr/bin/env python
#coding: utf-8
 
import logging
import os.path
 
DEBUG = True
TEMPLATE_DEBUG = DEBUG
HERE = os.path.dirname(os.path.abspath(__file__))
 
ADMINS = (
    # ('Your Name', 'your_email@example.com'),
)
 
MANAGERS = ADMINS
 
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'djangodemo',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '3306',                      # Set to empty string for default. Not used with sqlite3.
    }
}
 
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Asia/Shanghai'
 
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'zh-cn'
 
SITE_ID = 1
 
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
 
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True
 
MEDIA_ROOT = os.path.join(HERE, 'data').replace('\\','/')
 
STATIC_ROOT = os.path.join(HERE, 'static').replace('\\','/')
 
CAPTCHA_FONT=os.path.join(HERE,'static/Vera.ttf')
 
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
#MEDIA_ROOT = ''
 
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'
 
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
#STATIC_ROOT = ''
 
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
 
# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'
 
# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)
 
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#   'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
 
# Make this unique, and don't share it with anybody.
#www.iplaypy.com
SECRET_KEY = '3d5&166r)l@xd4zc-a$iuw3nkyi99ee4!k3bjhy)ly1i8pc*b9'
#UPLOAD SETTINGS
FILE_UPLOAD_TEMP_DIR = os.path.join(HERE, 'data/upload/').replace('\\', '/')
FILE_UPLOAD_HANDLERS = ("django.core.files.uploadhandler.MemoryFileUploadHandler",
 "django.core.files.uploadhandler.TemporaryFileUploadHandler",)
DEFAULT_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage'
# for user upload
ALLOW_FILE_TYPES = ('.jpg', '.jpeg', '.gif', '.bmp', '.png', '.tiff')
# unit byte
ALLOW_MAX_FILE_SIZE = 1024 * 1024
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    'django.template.loaders.eggs.Loader',
)
 
MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)
 
ROOT_URLCONF = 'urls'
 
TEMPLATE_DIRS = (
   os.path.join(HERE,'templates'),
)
 
TEMPLATE_CONTEXT_PROCESSORS = (  
    "django.core.context_processors.auth", 
    "d
2000
jango.core.context_processors.request",
    "django.core.context_processors.media", 
) 
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    'django.contrib.admindocs',
    'blog',
    'account',
    'news',
    'photo',
    'rbac',
)
 
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}
#mail
EMAIL_HOST = 'smtp.gmail.com'                   #邮件smtp服务器
EMAIL_PORT = '25'                                        #端口
EMAIL_HOST_USER = 'code***@gmail.com'  #邮件账户
EMAIL_HOST_PASSWORD = '*********'      #密码
EMAIL_USE_TLS = False

Python代码片段之Django静态文件URL的配置:文章来源地址https://www.toymoban.com/news/detail-606912.html

#!usr/bin/env python
#coding: utf-8
from django.conf.urls.defaults import patterns, include, url
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
 
urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'djangodemo.views.home', name='home'),
    # url(r'^djangodemo/', include('djangodemo.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'^$', 'account.views.index',name="index"),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^blog/', include('blog.urls')),
    url(r'^account/', include('account.urls')),
    url(r'^news/', include('news.urls')),
    url(r'^photo/', include('photo.urls')),
    url(r'^rbac/', include('rbac.urls')),
    url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_ROOT}),
    url(r'^media/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.FILE_UPLOAD_TEMP_DIR}),
)

到了这里,关于Python代码片段之Django静态文件URL的配置的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 局域网管理软件Python示例代码片段

    局域网管理软件是企业和组织中管理和监控网络设备的关键工具。这些软件可以帮助管理员轻松地管理局域网中的设备,确保网络的稳定性和安全性。在本文中,我们将探讨局域网管理软件的重要性,并提供一个简单的示例代码来演示如何使用Python编写一个基本的局域网设备

    2024年02月08日
    浏览(36)
  • Django基础入门⑮:更新书籍信息 删除书籍条目信息 Django静态文件配置

    🏘️🏘️个人简介:以山河作礼。 🎖️🎖️: Python领域新星创作者,CSDN实力新星认证,阿里云社区专家博主,CSDN内容合伙人 🎁🎁:Web全栈开发专栏:《Web全栈开发》免费专栏,欢迎阅读! 🎁🎁: 文章末尾扫描二维码可以加入粉丝交流群,不定期免费送书。 更改原有的书

    2024年02月16日
    浏览(21)
  • Python web实战之Django URL路由详解

      技术栈:Python、Django、Web开发、URL路由 Django是一种流行的Web应用程序框架,它采用了与其他主流框架类似的URL路由机制。URL路由是指将传入的URL请求映射到相应的视图函数或处理程序的过程。 URL路由是Web开发中非常重要的概念,它将URL映射到特定的视图函数。在Django中,

    2024年02月14日
    浏览(36)
  • Python Django 零基础从零到一部署服务,Hello Django!全文件夹目录和核心代码!

    在这篇文章中,我将手把手地教你如何从零开始部署一个使用Django框架的Python服务。无论你是一个刚开始接触开发的新手,还是一个有经验的开发者想要快速了解Django,这篇教程都会为你提供一条清晰的路径。我们将从环境搭建开始,一步一步地创建一个可以处理GET和POST请求

    2024年02月12日
    浏览(36)
  • Python学习之路:Django项目遇到ImportError: cannot import name ‘url‘ from ‘django.conf.urls‘解决方法(亲测有效)

    配置:Pthon 3.8.10-Django 4.1.1 使用命令创建数据库时: python manage.py migrate 提示错误:  from django.conf.urls import re_path as url ImportError: cannot import name \\\'re_path\\\' from \\\'django.conf.urls\\\' 经查阅相关资料,并实际操作,解决问题,具体办法往下: 修改生成项目下的urls.py文件中的:from django.c

    2023年04月21日
    浏览(50)
  • VSCode代码片段配置

    1.在VSCode设置 配置用户代码片段菜单添加  2.在输入框中选择新建代码片段   3.输入代码片段名称.例如:copyright 4. 生成第3步名称的代码片段文件,默认位置:C:Users 你的电脑名称 AppDataRoamingCodeUsersnippets 5. 代码片段模板的解释如下: 重点关注scope+prefix+body参数的配置,      

    2024年02月04日
    浏览(30)
  • python 获取阿里云oss文件分享url

    阿里云文档链接:前言 在阿里云把所有东西都配好之后按照代码填写对应的 AccessKeyId、yourAccessKeySecret等  

    2024年02月12日
    浏览(31)
  • Python学习笔记:Requests库安装、通过url下载文件

    在pipy或者github下载,通常是个zip,解压缩后在路径输入cmd,并运行以下代码  安装完成后,输入python再输入import requests得到可以判断时候完成安装  2.通过url下载文件 使用的是urllib模块

    2024年02月10日
    浏览(27)
  • python html(文件/url/html字符串)转pdf

    安装库 第二步 下载程序 wkhtmltopdf https://wkhtmltopdf.org/downloads.html 下载7z压缩包 解压即可, 无需安装 解压后结构应该是这样, 我喜欢放在项目里, 相对路径引用(也可以使用绝对路径, 放其他地方) 最好每个都像 string_to_pdf 函数一样, 捕获一下错误, 可以使程序更健壮, 避免转换失败

    2024年02月08日
    浏览(34)
  • Python 进阶 — Pylint 静态代码检查工具

    与 Flake8 一般,Pylint 也是一款 Python 的静态代码检查工具,它会分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码。除了平常代码分析工具的作用之外,Pylint 还提供了更多的功能,如:检查一行代码的长度,变量名是否符合命名标准,一个声明过的接口

    2023年04月08日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包