Giter VIP home page Giter VIP logo

django_templating's Introduction

Django_Templating

The following are steps to take after Django has been installed

Directories to create

  • 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)

Django Debug Toolbar

  • 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 your local_settings.py

Capturing variables in URL Routes

*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>

Custom Filters and Tags

  • 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

DRY Templates

  • 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 %}

Working with Images

  • 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

django_templating's People

Contributors

snamstorm avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.