Just some perspectives from a new user starting to use Django from Rails
and PHP. I really like the organization of Django (great job guys).
But due to some of the newness, here are some lack of examples that
still make Django kind of hard to use or figure out. So while my
comments might sound trivial or even simplistic, they are really
important for newcomers like myself.
ps: I think James Bennett's article on "Building a To-Do List in 30
Minutes" is a great beginner tutorial but should be revised to include
using NewForms instead of the Admin, include how to unittest the models
(I've included a write-up below), and
finally include how to deploy it up to a server. Even better would be a
short paragraph on how to set up your project with Mecurial, which is a
bit off-track, but still a necessary step in the development process.
Startup:
It is important to note in the documentation that with some distros (ie
ubuntu) you need:
$ django-admin.py startproject myproject
then to create an application:
$ cd myproject
$ python manage.py startapp myapp
Models:
Under the documentation, examples of how to specify fieldtypes and
default values seem to be missing - Under "examples" it has how-to's
which are very good but geared more towards api commands not simple
field definitions
FloatField - seems to be changing from 0.96 but are max_digits or
decimals still required ? If we do want to specifiy decimals will it
still be the same ?
DateField - how do I specify a default date field of today ? '01/01/01' ?
DateTimeField - how do I specify a certain date and time ?
URLS:
the examples listed in the urls.py file really shouldn't list the
"include(/...)" which is more advanced - it should just use a simple
example like:
"(r'^mymodel/list/$', 'myproject.myapp.views.mymodel_listing'),"
CRUD Stuff:
This is really important since it is the basis of most applications -
admin is nice for certain applications, but most applications are going
to have forms. NewForms seems the way to go, especially
"form_for_model". But there isn't a clear example of how to use this -
in fact, I assume the code goes into views.py but no mention of this is
made.
I tried the following but it fails in flames with "http response
required..."
urls.py
r'^mymodel/addedit/$', 'myproject.myapp.views.mymodel_addedit'),
views.py
def mymodel_addedit(request, id=None):
if id is None:
MyModelForm = forms.models.form_for_model(MyModel)
else:
mymodel = MyModel.objects.get(id=id)
MyModelForm = forms.models.form_for_instance(mymodel)
#MyModelForm.fields['detail'].widget = TinyMCE()
if request.method == 'POST':
form = MyModelForm(request.POST)
if form.is_valid():
mymodel = form.save(commit=False)
if id is None:
mymodel.owner = request.user
mymodel.save()
return HttpResponseRedirect("/")
else:
form = MyModelForm()
t = loader.get_template('mymodel_addedit.html')
c = Context({
'form': form,
'html_head': '<script src="/media/tiny_mce/tiny_mce_src.js"
type="text/javascript"></script>'
})
return HttpResponse(t.render(c))
/templates/mymodel_addedit.html
{% extends "base.html" %}
{% block content %}
<form action="." method="post">
<table class="form">
{{ form }}
</table>
<input type="submit" value="Submit" />
</form>
{% endblock %}
Deployment:
Many people use shared hosting (for example we use 1and1). So how do I
deploy to a typical shared hosting environment where I probably can't
change the http.conf
file but can use .htaccess file (I'm assuming this can be done - if it
can't then right up front on the Django web page, it ought to tell
people what hosting setup is required).
*********************************************
(this can be added to documentation / James Bennett's article)
Testing Your Models
Now that you have created a few models, it is important to create unit
tests to insure they work. This becomes more important later when you
make changes to your application and want to insure the changes work
before you upload them to the web. Django has built in unit testing
that makes this easy.
In your application directory, you need to create a "tests.py" file. In
this file you can include the following code:
import unittest
from todo.models import List
from todo.models import Item
class ListTestCase(unittest.TestCase):
def setUp(self):
self.testlist = List.objects.create(name='TestList')
def test_Name(self):
self.assertEquals(self.testlist.name, 'TestList')
class ItemTestCase(unittest.TestCase):
def setUp(self):
self.testlist = List.objects.create(name='TestList')
self.testitem = Count.objects.create(name="TestItem",
todo_list = self.testlist)
def test_Name(self):
self.assertEquals(self.testitem.name, 'TestItem')
The "def setUp(self)" function can be used to create an instance of the
model that can be used in the test functions that follow. So for the
List model, we create an instance named "testlist" and have set the
object name field with "TestList".
Actual test functions begin with "test" (an underscore is not necessary
but makes for easy code reading). In the test function, different
"assert" functions can be used - in this case an "assertEquals" function
is used to make a comparison of the name field in the created object
(testlist) with the actual phrase "TestList".
Now you run the tests at the command line by typing:
python manage.py test
If the object was created correctly, the names should match and you
should get a test passed response like :
Ran 2 tests in 0.003s
OK
otherwise Python and Django gives you pretty specific error messages.
Note that for the Item class test, you need to create an instance of a
List object, then create an Item object and fill in the "todo_list"
foreign key field with the name of the newly created list object. Note
that an object created in the first class does not extend to the 2nd
class. You don't need to create separate classes for each model but it
results in cleaner testing and coding.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---