时间:2023-06-08 13:39:02 | 来源:网站运营
时间:2023-06-08 13:39:02 来源:网站运营
Django-从0到1写出一个全面的网页(1)模板和静态图片: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代码很简单,标题是Eva, 主体标题是Eva says....., 内容是hello world!,大写加粗。然后是一个现在还用不到的about链接。python3 manage.py startapp polls
我这里用的是python3 manage.py startapp eva
) from django.shortcuts import render
现在在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/' #这个应该已经有了STATICFILES_DIRS = ( STATIC_PATH,)
这里的STATIC_URL 代表了基本的URL,当你的服务器运行时候,你的Django 程序会发现你的静态媒体文件。<!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 模板系统知道我们再用静态媒体。<!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)
当你的urls.py文件更新完成以后,我们需要修改一下settings.py 文件;我们需要设置两个变量的值,MEDIA_URL 和 MEDIA_ROOT#Media MEDIA_URL = '/media/'MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
一切顺利的话,我们需要做的就是把一张图片放入我们创建的 media文件夹里,然后在浏览器中输入http://127.0.0.1:8880/media/django.jpg
我这里用的port 是8880,只要保持和你运行主程序相同的port 就可以。如果能看到图片,恭喜你,成功啦!关键词:模板,和静,图片