Pages

Saturday, March 16, 2013

You should always issue a redirect for successful POST requests


The reason: if a user hits “Refresh” on a page that was loaded via POST, that request will be repeated. This can often lead to undesired behavior, such as a duplicate record being added to the database – or, in our example, the e-mail being sent twice. If the user is redirected to another page after the POST, then there’s no chance of repeating the request.
You should always issue a redirect for successful POST requests. It’s a Web development best practice.

blank=True null=True

So that’s a long way of saying this: if you want to allow blank values in a date field (e.g., DateFieldTimeField,DateTimeField) or numeric field (e.g., IntegerFieldDecimalFieldFloatField), you’ll need to use bothnull=True and blank=True.

Friday, March 15, 2013

Adding your models to the Admin Interface


from django.contrib import admin
from mysite.books.models import Publisher, Author, Book

admin.site.register(Publisher)
admin.site.register(Author)
admin.site.register(Book)

Indent error def __unicode__(self):


Your exact indentation you've pasted wouldn't throw that error, but in your real code, you must have thedef __unicode__ line at the exact indent depth as the other lines in your model.
Make sure you are using spaces and not tabs for all of your indents, as tabs can sometimes make the indent level seem the same as the others.

No module named books

Thanks for the help everyone. I was using 1.4.x, not the 1.0 the book is based on. After fooling around with the commands for a bit, I was able to figure out that the problem was in the INSTALLED_APPS section - I included 'mysite.books', but the new way to do that is just 'books'.

Instead of mysite.books in the INSTALLED_APPS write books

sqlite3.OperationalError: unable to open database file

I'm a beginner, too, and had a similar problem. The .db file will created for you when you run python manage.py syncdb. From your terminal window, just be sure that you're located in the same folder as the manage.py file in your directory structure. From this location, I was able to successfully create the necessary tables. The settings.py file will have already been created when you initially set the project up.


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'mydata.db',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

Database will be created once you do python manage.py syncdb

Finally to check if everything is fine

from django.db import connection
cursor = connection.cursor()

If nothing happens, then your database is configured properly. Otherwise, check the error message for clues about what's wrong.

Monday, March 4, 2013

ls -lh
finding out the sizes of different files in a folder

wc -l
finding out the number of lines in a file

head -10
top 10 lines of a file