Proceeding with my project to learn how to develop a Django web application over Pinax, I build two simple forms: one for submitting a new item, and another to show an item.
As one reader commented, up to this point I’m not really using any Pinax feature other than the website structure itself, like authentication, templates and menus. I’m starting with a basic Django application. I hope to add soon features like notification, messaging, tagging, gravatar.
So, in this sprint I started by defining two URLs:
- /pastebin/ to submit a new item
- /pastebin/<uuid>/ to view a submitted item
This is what my apps/oxybeles/urls.py file looks like:
from django.conf.urls.defaults import *
from oxybeles.models import PastedItem
info_dict = {
'queryset': PastedItem.objects.all(),
'slug_field': 'uuid',
}
urlpatterns = patterns('',
url(r'^$', 'oxybeles.views.new', name='oxybeles_new'),
url(r'^(?P<slug>[-0-9a-f]{36})/$',
'django.views.generic.list_detail.object_detail',
info_dict,
'oxybeles_detail'),
)
I also updated apps/oxybeles/models.py so it knows how to build a URL for a pasted item:
def get_absolute_url(self):
return ('oxybeles_detail', (), { 'slug': self.uuid })
get_absolute_url = models.permalink(get_absolute_url)
I wrote a simple form class in apps/oxybeles/forms.py:
from django import forms
from django.utils.translation import ugettext_lazy as _
from oxybeles.models import PastedItem
class PastedItemForm(forms.ModelForm):
class Meta():
model = PastedItem
fields = ('text',)
def __init__(self, user = None, *args, **kwargs):
self.user = user
super(PastedItemForm, self).__init__(*args, **kwargs)
And finally I wrote in apps/oxybeles/views.py the view function that is in charge of the form for submitting new items:
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, get_host
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext, ugettext_lazy as _
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from oxybeles.models import PastedItem
from oxybeles.forms import PastedItemForm
def new(request, form_class=PastedItemForm, template_name="oxybeles/new.html"):
"""
Form for pasting new items.
"""
form = form_class()
if request.method == 'POST':
if request.POST["action"] == "paste":
form = form_class(request.user, request.POST)
if form.is_valid():
item = form.save(commit=False)
item.user = request.user
item.save()
request.user.message_set.create(
message=ugettext("The new pasted item was saved."))
# some problem with ugettext_lazy here
return HttpResponseRedirect(reverse('oxybeles_detail',
args=(item.uuid,)))
return render_to_response(template_name,
{ "form": form, },
context_instance=RequestContext(request))
new = login_required(new)
Finally, I wrote the two templates.
templates/oxybeles/new.html:
{% extends "site_base.html" %}
{% load i18n %}
{% block head_title %}{% trans "Paste Bin" %}{% endblock %}
{% block body %}
<div id="basic-form">
<fieldset>
<legend>{% trans "New Item" %}</legend>
<form id="pastebin_new_form" method="POST" action="">
<div>{{ form.non_field_errors }}</div>
<div>{{ form.text.errors }}</div>
<div>{{ form.text }}</div>
<div><input type="hidden" name="action" value="paste" />
<input type="submit" value="paste" class="button" /></div>
</form>
</fieldset>
</div>
{% endblock %}
templates/oxybeles/pasteditem_detail.html:
{% extends "site_base.html" %}
{% load i18n %}
{% block head_title %}{% trans "Paste Bin" %}{% endblock %}
{% block body %}
<h1>Pasted Item</h1>
<pre>
<p>{{ object.text }}</p>
</pre>
{% endblock %}
And this is the final result:
http://127.0.0.1:8000/pastebin/

http://127.0.0.1:8000/pastebin/47d33482-a936-453a-8d4a-88aada4ebc44/

So, the basic app is in place. The source is in GitHub. In the next article I plan to implement a command to send a pasted item to a user, using Pinax’s features.