4. Templates and Static media
This chapter explains the template engine
4.1 Using templates
Before we explained the view and URL mappings and created the Django Web page, now we're going to mix the templates in
Good websites always have a lot of repetition on the layout. Django provides templates to achieve this design goal faster. Detach the logic from the page. In this chapter, a new template is created to create a new HTML page. This template is distributed through a Django view. We will use models to dynamically generate data and mix it in the template in order to achieve more in-depth learning.
4.1.1 Configuration Templates Folder
Create a new Templates folder under Tango_with_django_project. Create a new Rango folder, like Tango_with_django_project/templates/rango, which is created for Rango applications.
4.1.2 Dynamic Path
Once you've created the above folder, tell your Django project where you created the folder. So the configuration is done inside the settings.py.
First add the following code to the file header
Import OS
Settings_dir = Os.path.dirname (__file__)
Then add the following two code below
Project_path = Os.path.join (Settings_dir,os.pardir)
Project_path = Os.path.abspath (Project_path)
After setting the absolute path, add the following code to indicate the location of the Templates folder
Template_path = Os.path.join (project_path, ' template ')
Then configure the Template_dirs tuple
Template_dirs = (
Template_path,
)
4.1.3 Adding a template
After you have configured the path of your template folder, create a new index.html file under the Templates/rango folder with the following code:
<! DOCTYPE html>
<title>Rango</title>
<body>
Hello world!<strong>{{boldmessage}}</strong><br/>
<a href= "/rango/about/" >About</a><br/>
<body>
There is a {{boldmessage}} in the above code, which is a Django template variable, and we will set a value for the variable to show the output.
In order to use this template, we need to reconfigure index (), this time, not to display the text. Instead of showing the template, add the following code to the rango/views.py
From django.template import RequestContext
From django.shortcuts import Render_to_response
Update the index () view as follows
def index (Request):
Context = RequestContext (Request)
Context_dict = {' Boldmessage ': "I am bold font from the context"}
Return Render_to_response (' rango/index.html ', Context_dict,context)
Restart the server, enter http://127.0.0.1:8000/rango/, will see the effect
4.2 Providing static media
4.2.1 Create a static folder under Tango_with_django_project to hold files such as Css,js.
After the static folder is new, you have to tell Django where he is, just as you just created the template. Inside the setting.py file, update two variables, one static_url and staticfiles_dirs tuples. First create a new variable to store the path to the static folder.
Static_path = Os.path.join (Project_path, ' static ')
Static_url = '/static/'
Staticfiles_dirs = (
Static_path,
)
Static_url defines the root path of your Django application to access static resources, and defining errors can cause a lot of trouble.
STATICFILES_DIRS specifies where the static folder on your computer is placed. Just like Template_dirs tuples.
4.3 Static media files and templates
Now that you have access to those static files through your template, modify the index.html file as follows:
<! DOCTYPE html>
{% load static%}
<title>Rango</title>
<link rel= "stylesheet" href= "{% static" Css/base.css "%}" >
<script src= "{% static" Js/jquery.js "%}" ></script>
<body>
Hello world!<strong>{{boldmessage}}</strong><br/>
<a href= "/rango/about/" >About</a><br/>
The {% load static%} is used to declare a static file in the template that can be used in static form, such as {% static "rango.jpg"%}
</body>
4.4. Static Media Server
Many sites now offer their users the ability to upload, so you need to add a simple media server to your Django project.
Create a new media folder, which is the same level as static. After adding the folder, modify the urls.py file below tango_with_django_project/tango_with_django_project/to add the following code
From django.conf Import settings
#放在urlpatterns下面
If settings. DEBUG:
Urlpatterns + = Patterns (
' Django.views.static ',
(R ' media/(? p<path>.*) ',
' Serve ',
{' Document_root ': Settings. Media_root})
)
Django.conf's settings module allows us to access variables in the project's settings file. The optional condition is to check if the Django project is running in debug mode. If so, then the URL below will be added to the tuple above. This pattern represents any URL request that begins with media and is directed to the Django.views.static view. This view will handle the dispatch of the uploaded file for you.
Also need to modify the configuration inside the settings.py file, add the following code:
Media_url = '/media/'
Media_root = Os.path.join (Project_path, ' media ')
4.5 Summary
In this chapter, learn how to use templates in a view, use static media files, and upload files.
"Translation" How to Tango with Django 1.5.4 Fourth chapter