This is the third of a series of articles about my experience with the Pinax project. I am building a sample paste bin application named Oxybeles, of all things.
On the previous articles I installed Pinax and created a tab in the main menu for my new app. Now I want to create a basic view, but first I need to start a new Django app.
$ python manage.py startapp oxybeles
It seems that it would be appropriate to move it into the apps directory, so I did that:
$ mv oxybeles/ apps/
Here, I took a detour to create a GitHub repository to host this application, but I won’t record my git sessions on this series, because the focus is on Pinax and Django.
After exploring a bit how other Pinax application URLs are set up, I decided to start by copying the pattern used in the “about” application.
I started by creating a basic template:
$ mkdir templates/oxybeles $ gedit templates/oxybeles/new.html
I created the new file templates/oxybeles/new.html with this content:
{% extends "site_base.html" %}
{% load i18n %}
{% block head_title %}{% trans "Paste Bin" %}{% endblock %}
{% block body %}
{% blocktrans %}
<p>This will be a form to post some text.</p>
{% endblocktrans %}
{% endblock %}
Next, I created a new urls.py file inside the new app:
$ gedit apps/oxybeles/urls.py
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
url(r'^$',
direct_to_template,
{"template": "oxybeles/new.html"},
name="oxybeles_new"),
)
Then I added the new application to the main urls.py file:
$ gedit urls.py
Around line 56, inside the urlpatterns list declaration, I inserted:
(r'^pastebin/', include('oxybeles.urls')),
And finally, I changed the menu option to link to the new URL:
$ gedit templates/site_base.html
Changing:
<td class="tab rtab_pastebin"><div><a href="#">{% trans "Paste Bin" %}</a></div></td>
To:
<td class="tab rtab_pastebin"><div><a href="{% url oxybeles_new %}">{% trans "Paste Bin" %}</a></div></td>
Now the Paste Bin menu item links to http://127.0.0.1:8000/pastebin/ and that renders the oxybeles/new.html template that shows just:
This will be a form to post some text.
Good, the new app is linked to Pinax and the basic view is working. In the next article I’ll try to get a basic form working.
The application source code at this stage can be found in GitHub.
If one skips the steps
$ python manage.py startapp oxybeles
$ mv oxybeles/ apps/
You will need to put the __init__.py file in the new ../apps/project_name directory or you will get an error complaining about no “project_name.urls”.
Comment by mgag — November 13, 2008 @ 10:12 pm
mgag: That’s right. The startapp command is really a shortcut to create a python module and a few Django application stub files. __init__.py is Python’s way of saying that a directory is a module. A tip: in Django, projects and applications are different concepts. So “apps/project_name” is not a very good example. You may say that a project is a collection of applications.
Comment by fernandoacorreia — November 13, 2008 @ 10:25 pm
I would think you re forgetting to add your new app to the settings.py file under INSTALLED_APPS…
Comment by PhilGo20 — November 20, 2009 @ 6:34 pm