The following are steps to take after Django has been installed
-
Add a new directory in your app called 'templates' (All HTML files go here)
-
Add to your
settings.py
:TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'your_app/templates')].
-
Create a python package called
templatetags
in your application. (For Filters and Tags) -
Create a folder in your application called 'static' (Where HTML files live)
-
In your 'static' folder create three folders called 'js', 'css', 'img' (static files that link to HTML files)
pip install django-debug-toolbar
- Put
debug_toolbar<c/ode> in your INSTALLED_APPS.
- Put
INTERNAL_IPS = ("127.0.0.1", "10.0.2.2")
in yourlocal_settings.py
*URL'S and Regex URL's: add the following code to urls.py
urlpatterns = patterns('',
# Variables are captured in the part of the url in parentheses
# The '?P<variable_name>' syntax lets you name the variable
# The regex after the variable's name indicates what is allowed
url(r'^URL/(?P<some_variable>\w+)$', 'your_app.views.URL'),
)
- Link your URL's to
views.py
from django.shortcuts import render_to_response
def URL(request, some_variable):
return render_to_response(
"file_name.html",
{'some_variable': some_variable}
)
- create
file_name.html
in 'templates'
<body>
<h1>Hello {{ some_variable }}!</h1>
</body>
- Create a file in templatetags called
some_file.py
- Add the following code to the file:
from django import template
register = template.Library()
# recognize this function as a filter
@register.filter
def func(condition):
- load the filter in pages using this filter by including
{% load some_file %}
on the first line
- create a new template called header.html
- In the header template, include (and any other files that need to be run across all pages):
<head>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="{% static 'js/cards.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'css/cards.css' %}">
</head>
<body>
<h1>Welcome to our Card Game!</h1>
{% block content %}{% endblock content %}
</body>
- Include the header.html file in other files by extension
{% extends 'header.html' %}
{% block content %}
<p>YOUR HTML TEXT HERE</p>
{% endblock content %}
- pip install pillow <- this will let our python code work with images.
- In
urls.py
add the following at the bottom of the file:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And import from:
from django.conf import *
from django.conf.urls.static import *
- In settings.py add the following before the line that imports your local_settings file:
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
MEDIA_URL = "/img/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, "static", *MEDIA_URL.strip("/").split("/"))
- Add {% load staticfiles %} to the top of the template that includes images