defprocess_request(self, request): ifself.enabled andnot request.is_secure(): for path inself.paths: if request.get_full_path().startswith(path): request_url = request.build_absolute_uri(request.get_full_path()) secure_url = request_url.replace('http://', 'https://') return HttpResponsePermanentRedirect(secure_url) returnNone
In /home/username/public_html/projectname/projectname/settings.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
MIDDLEWARE_CLASSES = ( ... # the line below if not comment out, 403 error will occur and I still haven't figure it out # 'django.middleware.csrf.CsrfViewMiddleware', ... 'middleware.SecureRequiredMiddleware', ) ... # HTTPS MIDDLEWARE CONFIG HTTPS_SUPPORT = True SECURE_REQUIRE_PATHS = ( '/', '/admin/', # you can add in more path here if you want the path to use https )
from django.template.loader import get_template from django.template import Context from django.http import HttpResponse import datetime
defindex(request): now = datetime.datetime.now() t = get_template('index.html') # pass a dynamic value to the view html = t.render(Context({'special_date': now})) return HttpResponse(html)
In /home/username/public_html/projectname/newapp/urls.py
1 2 3 4 5 6
from django.conf.urls import patterns, url from newapp import views
First, add the template dir to /home/username/public_html/projectname/projectname/settings.py
1 2 3 4 5 6 7
import os.path ... TEMPLATE_DIRS = ( ... # in case that it is run on windows, so have to change to forward slash no matter how os.path.join(os.path.dirname(__file__), 'newapp/templates'.replace('\\', '/')), )
Create a directory to keep all templates related to newapp