時間:2023-06-08 13:39:02 | 來源:網(wǎng)站運(yùn)營
時間:2023-06-08 13:39:02 來源:網(wǎng)站運(yùn)營
Django-從0到1寫出一個全面的網(wǎng)頁(1)模板和靜態(tài)圖片:TEMPLATES = [ { 'DIRS': ['<workspace>/zhou/', ], }]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
這是用來存儲指向你項目中settings.py 模塊所在目錄的路徑。TEMPLATES = [ { 'DIRS': ['<workspace>/zhou/', TEMPLATE_PATH, BASE_DIR + '<workspace>/zhou/templates/rango', ], }]
什么是TEMPLATEPATH呢?我們可以在settings.py 文件的開頭,BASE_DIR 下面闡明。# Build paths inside the project like this: os.path.join(BASE_DIR, ...)BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
<!DOCTYPE html><html> <head> <title>Eva</title> </head> <body> <h1>Eva says...</h1> hello world! <strong>{{ boldmessage }}</strong><br /> <a href="/rango/about/">About</a><br /> </body></html>
這段HTML代碼很簡單,標(biāo)題是Eva, 主體標(biāo)題是Eva says....., 內(nèi)容是hello world!,大寫加粗。然后是一個現(xiàn)在還用不到的about鏈接。python3 manage.py startapp polls
我這里用的是python3 manage.py startapp eva
) from django.shortcuts import render
現(xiàn)在在views.py 文件中,更新下index() view 程序def index(request): # Construct a dictionary to pass to the template engine as its context. # Note the key boldmessage is the same as {{ boldmessage }} in the template! context_dict = {'boldmessage': "I am bold font from the context"} # Return a rendered response to send to the client. # We make use of the shortcut function to make our lives easier. # Note that the first parameter is the template we wish to use. return render(request, 'rango/index.html', context_dict)
記得保存哦!# Static files (CSS, JavaScript, Images)# Managing static files (e.g. images, JavaScript, CSS)STATIC_PATH = os.path.join(BASE_DIR,'static')STATIC_URL = '/static/' #這個應(yīng)該已經(jīng)有了STATICFILES_DIRS = ( STATIC_PATH,)
這里的STATIC_URL 代表了基本的URL,當(dāng)你的服務(wù)器運(yùn)行時候,你的Django 程序會發(fā)現(xiàn)你的靜態(tài)媒體文件。<!DOCTYPE html>{% load staticfiles %}<!-- 新加第一行 --><html> <head> <title>Eva</title> </head> <body> <h1>Eva says...</h1> hello world! <strong>{{ boldmessage }}</strong><br /> <img src="{% static "images/django.jpg" %}" alt="Picture of Django" /> <!-- 新加第二行 --> <a href="/rango/about/">About</a><br /> </body></html>
新加的第一行讓我們的Django 模板系統(tǒng)知道我們再用靜態(tài)媒體。<!DOCTYPE html>{% load staticfiles %}<html> <head> <title>Eva</title> <link rel="stylesheet" href="{% static "css/base.css" %}" /> <!-- CSS --> <script src="{% static "js/jquery.js" %}"></script> <!-- JavaScript --> </head> <body> <h1>Eva says...</h1> hello world! <strong>{{ boldmessage }}</strong><br /> <img src="{% static "images/django.jpg" %}" alt="Picture of Django" /> <!-- New line --> </br> <a href="/rango/about/">About</a><br /> </body></html>
我順便使用了</br> 空了一格,讓about 鏈接格式顯得更好看一點。from django.conf import settingsfrom django.conf.urls.static import staticif settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
當(dāng)你的urls.py文件更新完成以后,我們需要修改一下settings.py 文件;我們需要設(shè)置兩個變量的值,MEDIA_URL 和 MEDIA_ROOT#Media MEDIA_URL = '/media/'MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
一切順利的話,我們需要做的就是把一張圖片放入我們創(chuàng)建的 media文件夾里,然后在瀏覽器中輸入http://127.0.0.1:8880/media/django.jpg
我這里用的port 是8880,只要保持和你運(yùn)行主程序相同的port 就可以。如果能看到圖片,恭喜你,成功啦!關(guān)鍵詞:模板,和靜,圖片
客戶&案例
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。