Django Workflow
administration:
Create the administrative user to enable logins to the /admin section of the site:
(env)$ python manage.py createsuperuser
View & URL
- django-admin.py startapp <appname>
- add the app to the projects settings.py file section:
<span class="n">INSTALLED_APPS</span> <span class="o">=</span> <span class="p">(</span> <span class="s">'django.contrib.admin'</span><span class="p">,</span> <span class="s">'django.contrib.auth'</span><span class="p">,</span> <span class="s">'django.contrib.contenttypes'</span><span class="p">,</span> <span class="s">'django.contrib.sessions'</span><span class="p">,</span> <span class="s">'django.contrib.messages'</span><span class="p">,</span> <span class="s">'django.contrib.staticfiles'</span><span class="p">,</span> <span class="s">'<appname>'</span><span class="p">,</span> <span class="p">) </span>
-
Create a view to render: edit the <appname> apps views.py file: ``` from django.http import HttpResponse
def index(request): return HttpResponse(“It works!”) ```
-
Map the URL for the view: create the <appname> urls.py ``` from django.conf.urls import patterns, url from
</span>import views urlpatterns = patterns(’’, url(r’^$’, views.index, name=‘index’)) ```
-
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’)),
<span class="n">url</span><span class="p">(</span><span class="s">r'^admin/'</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">urls</span><span class="p">)),</span> <strong> <span class="n">url</span><span class="p">(</span><span class="s">r'^<appname>/'</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">'<appname>.urls'</span><span class="p">)),</span> </strong> <span class="p">)</span> ```
- 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:
- create a templates directory in the projects root
- inside of the new templates directory create a directory matching the name of your app <appname>
- inside of the /projectdir/templates/<appname>/ directory create an index.html file
Update the project settings.py:
- 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:
<span class="n">TEMPLATE_PATH</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">BASE_DIR</span><span class="p">,</span> <span class="s">'<projectname>/templates'</span><span class="p">)</span>
- then to the same file add:
<span class="n">EMPLATE_DIRS</span> <span class="o">=</span> <span class="p">(</span> <span class="c"># Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".</span> <span class="c"># Always use forward slashes, even on Windows.</span> <span class="c"># Don't forget to use absolute paths, not relative paths.</span> <span class="n">TEMPLATE_PATH</span><span class="p">,</span> <span class="p">)</span>
-
create a template view in the /projectdir/templates/<appname>/index.html file eg: ```
Print Consumable Tracking Tracking Printer Consumable Items
About
```
- in the application’s views.py file add: ```
from django.shortcuts import render
and update the index() to:
def index(request):
<span class="c"># Construct a dictionary to pass to the template engine as its context.</span> <span class="c"># Note the key boldmessage is the same as in the template!</span> <span class="n">context_dict</span> <span class="o">=</span> <span class="p">{</span><span class="s">'boldmessage'</span><span class="p">:</span> <span class="s">"I am bold font from the context"</span><span class="p">}</span> <span class="c"># Return a rendered response to send to the client.</span> <span class="c"># We make use of the shortcut function to make our lives easier.</span> <span class="c"># Note that the first parameter is the template we wish to use.</span> <span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s">'<appname>/index.html'</span><span class="p">,</span> <span class="n">context_dict</span><span class="p">)</span> ```
</body></html>