Django Workflow

administration:

Create the administrative user to enable logins to the /admin section of the site:

(env)$  python manage.py createsuperuser

View & URL

  1. django-admin.py startapp <appname>
  2. add the app to the projects settings.py file section:
    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        '<appname>',
    )
    
  3. Create a view to render: edit the <appname> apps views.py file:
    from django.http import HttpResponse
    
    def index(request):
        return HttpResponse("It works!")
  4. Map the URL for the view: create the <appname> urls.py
    from django.conf.urls import patterns, url
    from <appname> import views
    
    urlpatterns = patterns('',
            url(r'^$', views.index, name='index'))
  5. Update the project’s urls.py  file to look for the <appname>’s url in the request
    urlpatterns = patterns('',
        # Examples:
        # url(r'^$', 'tango_with_django_project_17.views.home', name='home'),
        # url(r'^blog/', include('blog.urls')),
    
        url(r'^admin/', include(admin.site.urls)),
        url(r'^<appname>/', include('<appname>.urls')), 
    )
  6. Now you can visit <IP>:8080/<appname> to see your view working (do restart the server first via:
    python manage.py runserver <IP>:8080 )

Templates

Create the directory structure for the app:

  1. create a templates directory in the projects root
  2. inside of the new templates directory create a directory matching the name of your app <appname>
  3. inside of the /projectdir/templates/<appname>/ directory create an index.html file

Update the project settings.py:

  1. edit the projects settings.py file and add the following where <projectname> is the name of the folder that houses the entire project and contains the manage.py file:
    TEMPLATE_PATH = os.path.join(BASE_DIR, '<projectname>/templates')
  2. then to the same file add:
    EMPLATE_DIRS = (
        # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
        # Always use forward slashes, even on Windows.
        # Don't forget to use absolute paths, not relative paths.
        TEMPLATE_PATH,
    )
  3. create a template view in the /projectdir/templates/<appname>/index.html file  eg:
    <!DOCTYPE html>
    <html>
    
    <head>
         <title>Print Consumable Tracking</title>
    </head>
    
    <body>
         <h1>Tracking Printer Consumable Items</h1>
         <strong>{{ boldmessage }}</strong><br />
         <a href="/rango/about/">About</a><br />
    </body>
    
    </html>
  4. in the application’s views.py file add:
    from django.shortcuts import render

    and update the index() to:

    def index(request):
    
        # Construct a dictionary to pass to the template engine as its context.
        # Note the key boldmessage is the same as {{ boldmessage }} in the template!
        context_dict = {'boldmessage': "I am bold font from the context"}
    
        # Return a rendered response to send to the client.
        # We make use of the shortcut function to make our lives easier.
        # Note that the first parameter is the template we wish to use.
    
        return render(request, '<appname>/index.html', context_dict)