django 顯示靜態檔案的幾種方式
這個問題還整了半天,問了兄弟波仔,終於解決!
先看效果!
程式碼如下:
[[email protected] templates]# more base.html
{% load staticfiles %}
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<style type="text/css">
#body{color:#efd;background:#000000;padding:0 5em;margin:0}
body{color:#efd;background:url("{% static "images/background.gif" %}");padding:0 5em;margin:0}
#body {background: white url("images/background.gif") no-repeat right bottom;}
h1{padding:2em 1em}
#h1{padding:2em 1em;background:#080000}
h2{color:#bf8;border-top:1px dotted #fff;margin-top:2em}
p{margin:1em 0;color:#080067}
</style>
</head>
<body>
{% load static %}
<a href="http://www.baidu.com"><img src="{% static "images/sitelogo.png" %}" alt="static type" /></a>
<a href="http://www.baidu.com"><img src="{{STATIC_URL}}images/sitelogo.png" alt="static_url type" /></a>
<a href="http://www.baidu.com"><img src="/jingtai/images/sitelogo.png" alt="abs type" /></a>
{% block content %}{% endblock %}
</body>
</html>
[ [email protected] templates]# pwd
/root/Desktop/data/download/django/mysite16_5_demo4/news/templates
[[email protected] templates]#
顯示效果如下
先看第一種顯示方式:
<a href="http://www.baidu.com"><img src="{% static "images/sitelogo.png" %}" alt="static type" /></a>
使用tag的形式
普通配置即可,如下:
[[email protected] mysite16_5_demo]# pwd
/root/Desktop/data/download/django/mysite16_5_demo4/mysite16_5_demo
[[email protected] mysite16_5_demo]# tail -10 settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
MEDIA_ROOT = '/headImg'
MEDIA_URL = '/data/'
STATIC_ROOT='/54Static'
#STATIC_URL = '/static/'
STATIC_URL='/jingtai/'
[ [email protected] mysite16_5_demo]#
html解析如下
再看第三種顯示方式,直接寫全路徑!setting.py配置同上!缺點是不易於維護
主要是第二種顯示方式
setting配置還是同上,但是圖片總是顯示不出來!
顯示效果如下:
修改顯示的方法
#year
def year_archive(request, year):
a_list = Article.objects.filter(pub_date__year=year)
return render_to_response('year_archive.html', {'year': year, 'article_list': a_list},RequestContext(request))
將return render_to_response('year_archive.html', {'year': year, 'article_list': a_list})
改為
return render_to_response('year_archive.html', {'year': year, 'article_list': a_list},RequestContext(request))
增加
RequestContext(request)
記得引用
from django.template import RequestContext
---add 2015年8月13日 17:57:45
顯示靜態檔案
urls.py配置如下
[[email protected] mysite16_5_demo]# more urls.py
from django.conf.urls import patterns, include, url
import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite16_5_demo.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
#(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': 'media'}),
(r'^articles/(\d{4})/$', 'news.views.year_archive'),
url(r'^news/', include('news.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^comm/',include('comm.urls')),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT }),
url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_ROOT}),
)
[[email protected] mysite16_5_demo]#
add 2015年8月14日 01:00:08
如果我寫的路徑不是static,而是media呢,我該怎麼顯示
按照之前的思路,我應該這樣改就可以了呀
<a href="http://www.baidu.com"><img src="{% static "images/sitelogo.png" %}" alt="static type" /></a>
<a href="http://www.baidu.com"><img src="{{MEDIA_URL}}upload/5.jpg" alt="static_url type" /></a>
<a href="http://www.baidu.com"><img src="/data/upload/5.jpg" alt="abs type" /></a>
但是除了第一個,其他的都沒有正確顯示,難道哪裡錯了,按照邏輯來說的話應該沒變呀
後臺列印的是
[12/Aug/2015 23:16:28] "GET /data/upload/5.jpg HTTP/1.1" 404 2533
難道是找不到這個路路徑,將settings.py 中的MEDIA_URL='/media/'時
同事修改模版為
<a href="http://www.baidu.com"><img src="{% static "images/sitelogo.png" %}" alt="static type" /></a>
<a href="http://www.baidu.com"><img src="{{MEDIA_URL}}upload/5.jpg" alt="static_url type" /></a>
<a href="http://www.baidu.com"><img src="/media/upload/5.jpg" alt="abs type" /></a>
圖片正常顯示
其實原因為我urls.py的配置
後臺列印為
[12/Aug/2015 23:21:36] "GET /news/articles/2014/ HTTP/1.1" 200 14799
[12/Aug/2015 23:21:36] "GET /jingtai/images/sitelogo.png HTTP/1.1" 304 0
[12/Aug/2015 23:21:36] "GET /jingtai/images/background.gif HTTP/1.1" 304 0
[12/Aug/2015 23:21:36] "GET /media/upload/5.jpg HTTP/1.1" 304 0
相關推薦
django 顯示靜態檔案的幾種方式
這個問題還整了半天,問了兄弟波仔,終於解決! 先看效果! 程式碼如下: [[email protected] templates]# more base.html {% load staticfiles %} <html> <head>
$ Django 調API的幾種方式
API呼叫方式 下面是python中會用到的庫。urllib2httplib2pycurlrequestsurllib2 #request import requests, json github_url = ” data = json.dumps({‘name’:’test’, ‘des
Asp.net下載檔案幾種方式
protected void Button1_Click(object sender, EventArgs e) { /* 微軟為Response物件提供了一個新的方法TransmitFile來解決使用Response.BinaryWrite 下載超過400mb的檔案時導致Aspnet_wp.
asp.net 下載檔案幾種方式
protected void Button1_Click(object sender, EventArgs e) { /* 微軟為Response物件提供了一個新的方法TransmitFile來解決使用Response.BinaryWrite 下載超過400mb的檔案時導
Nodejs 顯示圖片的幾種方式
var http = require('http') var fs = require('fs') var request = require('request') http .createServe
java讀取寫入檔案幾種方式效率比較
public class ReadTxtJson {public static String readTxtFile(String FileName) throws Exception {BufferedInputStream bufferedInputStream = n
頁面布局的幾種方式(靜態化布局,流式布局,自適應布局,響應式布局,彈性布局)
情況下 sea 手機 窗口大小 media sys tps 差異 媒體 一、靜態布局(static layout) 即傳統Web設計,網頁上的所有元素的尺寸一律使用px作為單位。 1、布局特點 不管瀏覽器尺寸具體是多少,網頁布局始終按照最初寫代碼時的布局來顯示。常規
django中csrftoken跨站請求偽造的幾種方式
urn pre 並不是 coo 註釋 cli bsp ucc UNC 1.介紹 我們之前從前端給後端發送數據的時候,一直都是把setting中中間件裏的的csrftoken這條給註釋掉,其實這個主要起了一個對保護作用,以免惡意性數據的攻擊。但是這樣直接註釋掉並不是理智型的
關於頁面之間區域性顯示的幾種方式
第一種:這裡使用到了easyui+ssm <body class="easyui-layout"> <div data-options="region:'west',title:'列表',split:true" style="width:200px;">
WPFの操作檔案瀏覽框幾種方式
原文: WPFの操作檔案瀏覽框幾種方式 方式1: 使用win32控制元件OpenFileDialog Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); ofd.DefaultExt
JavaScript~檔案下載的幾種方式
1.通過a標籤的方式來下載 <a href='' target='_blank'></a> 2.通過提交form表單的方式 var $form = $('<form action="'+url+'"
【Django】在檢視類使用裝飾器的幾種方式
在Django中,檢視中的類稱為類檢視,個人喜歡把檢視中的類叫做檢視類,函式叫做檢視函式,一種習慣而已。 一、定義檢視類 定義類檢視,且類檢視繼承自View(舉例) from django.views.generic import View class DemoVie
C# 寫入檔案的幾種方式
1. FileStream.Write string filePath = Directory.GetCurrentDirectory() + "\\" + Process.GetCurrentProcess().ProcessName + ".txt"; if (File.Exist
Java讀取resource檔案/路徑的幾種方式
方式一: String fileName = this.getClass().getClassLoader().getResource("檔名").getPath();//獲取檔案路徑 String fileUtl = this.getClass().getResource("檔名").getFi
bat批處理執行python 的幾種方式 ———— 批處理, python打包成 exe檔案
第一種方式: @echo off C: cd C:\Users\ldl\Desktop start python test100.py start python 1.py start python 1.py 10 start python 1.py 100 exit 第二種方
Python讀取WAV檔案的幾種方式整理
1)scipy from scipy.io import wavfile import numpy as np sample_rate, sig = wavfile.read('new.wav') print("取樣率: %d" % sample_rate) print(sig)
Python檔案操作中的a,a+,w,w+幾種方式的區別 ——轉載
轉載:https://blog.csdn.net/qq_38059635/article/details/81606977 第一步 排除檔案開啟方式錯誤: r只讀,r+讀寫,不建立 w新建只寫,w+新建讀寫,二者都會將檔案內容清零 (以w方式開啟,不能讀出。w+可讀
Python檔案操作中的a,a+,w,w+,rb+,rw+,ra+幾種方式的區別
access_mode:開啟方式,r讀,w寫,a追加,r+ w+ a+ 都是以讀寫方式開啟,rb二進位制讀,wb二進位制寫,rb+ wb+ ab+二進位制讀寫 buffering:預設值 二、對檔案進行操作 將檔案中的內容讀入到一個字串變數/列表中 函式:read(),
檔案上傳的幾種方式
一、springmvc中的檔案上傳 1.配置檔案 (1).pom檔案,檔案上傳主要需要如下幾個jar包 <dependency> <groupId>org.springframework</groupId>
Go實戰--golang中讀寫檔案的幾種方式
讀寫檔案應該是在開發過程中經常遇到的,今天要跟大家一起分享的就是在golang的世界中,如何讀寫檔案。 使用io/ioutil進行讀寫檔案 其中提到了兩個方法: func ReadFile func ReadFile(filename string) ([]by