django 2

来源:互联网 发布:心蓝软件 编辑:程序博客网 时间:2024/05/01 22:42

django 模板,视图,链接

<配合html5实现图片,音乐,视频的播放功能,以及网页的跳转>

  1. 在建立好django项目和添加一个应用之后,可以在同一个应用下面添加多个视图文件
    1.1 在当前工作目录下创建模板
    1.1.1 创建模板目录mkdir -p /proj/templates/myapp/
    1.1.2 创建两个模板文件 touch templ1.html templ2.html
    <两个为了实现网页跳转>
    1.1.3 模板的编写
    这里用的是html5 和 静态媒体
    其中注意:
    用静态媒体需要在文件头加上
    提供一个超链接 是两个模板的相对路径
    媒体资源对于python有固定的格式 {%static “相对路径”%}
    {% load staticfiles %}
    超链接
    {% load staticfiles %}
    <!DOCTYPE html>
    <html>
    <head>
    <title>itcastsubject</title>
    </head>
    <body>
    <h1>itcast says...</h1>
    hello world! <strong> {{ boldmessage }} </strong><br />
    <a href="/myapp2/index/">Index</a><br />
    <img src="{% static "images/666.jpg" %}" alt="beautifulgirl" >
    <br></br>
    <video width="640" height="360" controls>
    <source src="{% static "video/feicai.mp4" %}" type="video/mp4" >
    </video>
    <audio controls>
    <source src={% static "audio/Moonlight.mp3" %} type="audio/mpeg">
    </audio>
    </body>
    </html>

    1.2 在对应的视图文件中添加渲染函数render
    def index(request):
    字典dict=xxxx(可以没有字典,模板中没有占位符的情况下)
    return render(request,"模板的相对路径,此处为myapp/templ1.html",dict)

    1.3 通过在对应app下的urls.py来设置路由
    这里写图片描述

    from django.conf.urls import patterns, url
    from myapp2 import views
    from myapp2 import about
    urlpatterns = patterns('',
    url(r'^$', views.showhello, name='showhello'),
    url(r'^showapp', views.showapp, name='showapp'),
    #url(r'^showhello', views.showhello, name='showhello'),
    url(r'^index', views.index, name='index'),
    url(r'^about', about.index, name='index'),
    )

注:

由于添加了静态媒体和模板
需要在项目的settings.py下添加设置
STATIC_URL = ‘/static/’

TEMPLATE_PATH=os.path.join(BASE_DIR,”templates”)
TEMPLATE_DIRS=[
TEMPLATE_PATH ,
]

STATIC_PATH = os.path.join(BASE_DIR,’static’)
STATICFILES_DIRS = (
STATIC_PATH,
)

0 0
原创粉丝点击