📜 ⬆️ ⬇️

Google App Engine: templates and statics (css, js, images)

In the previous post it was told how to create the simplest project on GAE + Django. Now let's try to connect to the project Django templates and support for static files - style sheets, scripts and images.

Our Django project will be called dvk , and contain the main application. In the application folder, create a directory for templates templates :
dvk /
main.py
app.yaml
dvk /
manage.py
settings.py
urls.py
main /
__init__.py
models.py
views.py
templates /



In order for the runtime environment to find the template files, you need to set the path to the templates folder in the project settings file settings.py .
import os
ROOT_PATH = os.path.dirname (__ file__)
')
TEMPLATE_DIRS = (
ROOT_PATH + "/ main / templates",
)


Now you can create a template file main.html in the templates folder. Our template will not do anything extra useful :) Just say browser "Hello world!"
Hello world!


To display the page, create a procedure controller in the views.py file and link it to the processing of the request for the main page of the site in urls.py
# dvk / dvk / main / views.py
from django.shortcuts import render_to_response

def index (request):
return render_to_response ("main.html")


# dvk / dvk / urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns ("",
(r "^ $", "dvk.main.views.index"),
)


Now you can test our application - dev_appserver.py dvk
Open a browser with the address 127.0.0.1 : 8080 / and you will see “Hello world!” :-)

In order for Google App Engine to process static files, you need to include the static_dir parameter in the handlers section of the handlers section:
application: dvk
version: 1
runtime: python
api_version: 1

...

handlers:
- url: / static
static_dir: static

...


The url parameter indicates the address at which files will be available, and the value of the static_dir parameter indicates the name and location of the folder. In our example, it is located at the root of the project and has the name static . Now you can refer to static files in templates, for example, to load a style sheet:
link href = "/ static / main.css" type = "text / css" rel = "stylesheet"
or drawing
img src = "/ static / logo.gif"

Used materials:
Django on Google App Engine: Templates and static files
Google App Engine - Configuring an App

Cross post from my blog

Source: https://habr.com/ru/post/26186/


All Articles