If you’ve just jumped in, this is my blog series of my walkthrough of a fantastic book on learning Django w/Python called “Django 1.0 Web Site Development” by Ayman Hourieh. I’m doing this to pick up a lot of good information: knowledge of Python, knowledge of Django, and knowledge of MVC. Nice that I can do all three in one book. The blog posts are so I don’t forget it and hopefully to highlight a great resource.
Our original page has HTML in the main page and that’s something we don’t want. So, now we’ll be taking it to the next level with Templates. Go to your project folder and create a folder called “templates”.
Now edit your settings.py
At the top add this like:
Import os.path
Look for this section:
TEMPLATE_DIRS = ( # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. )
Add this line inside:
os.path.join(os.path.dirname(__file__),'templates'),
Now create a main_page.html inside the templates folder and paste this in:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>{{ page_title }}</title> </head> <body> <h1>{{ page_title }}</h1> <p>{{ page_body }}</p> </body> </html>
Now let’s jump into bookmarks/views.py
from django.http import HttpResponse from django.template import Context from django.template.loader import get_template # Create your views here. def main_page(request): template = get_template('main_page.html') variables = Context({ 'head_title': u'Django Bookmarks', 'page_title': u'Welcome to Django Bookmarks', 'page_body': u'Where you can store and share bookmarks!' }) output=template.render(variables) return HttpResponse(output)
Great, now go back to your web page and hit refresh. You should see almost the same thing without the “seriously”.
Next I’m writing a view for the User to see his/her Links.
So, first we’ll create the URL:
Go to urls.py and add a new pattern line under the main page pattern:
(r'^user/(w+)/$', user_page),
Regular expressions sure are ugly looking, but they are so useful, you just have to use them J
Now go back to bookmarks/views.py to add the User Page view:
from django.http import HttpResponse, Http404 from django.template import Context from django.template.loader import get_template from django.contrib.auth.models import User # Create your views here. def main_page(request): template = get_template('main_page.html') variables = Context({ 'head_title': u'Django Bookmarks', 'page_title': u'Welcome to Django Bookmarks', 'page_body': u'Where you can store and share bookmarks!' }) output=template.render(variables) return HttpResponse(output) def user_page(request, username): try: user = User.objects.get(username=username) except User.DoesNotExist: raise Http404(u'Requested user was not found.') bookmarks = user.bookmark_set.all() template = get_template('user_page.html') variables = Context ({ 'username':username, 'bookmarks':bookmarks }) output=template.render(variables) return HttpResponse(output)
Time to design that new template. In templates/ add a page called user_page.html. Give it this code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Django Bookmarks - User: {{ username }}</title> </head> <body> <h1>{{ username }}'s Bookmarks</h1> {% if bookmarks %} <ul> {% for bookmark in bookmarks %} <li><a href="{{ bookmark.link.url }}">{{ bookmark.title }}</a></li> {% end for %} </ul> {% else %} <p>You have no bookmarks!</p> {% end if %} </body> </html>
Now you can browse to your original users page and see what’s going on:
Not a whole lot apparently so let’s jump back to the command prompt, open a database shell and fill in some real data:
If you’ve closed it, no worries, open a command prompt and browse to your PythonXX folder
Run these commands:
python manage.py shell >>> from django.contrib.auth.models import User >>> from bookmarks.models import * >>> user1 = User(username=u'mdhall') >>> user1.save() >>> user2 = User(username=u'lahall') >>> user2.save() >>> User.objects.all() [<User: sa>, <User: mdhall>, <User: lahall>] >>> user1 = User.objects.get(id=2) >>> link1 = Link.objects.get(id=1) >>> user1.bookmark_set.all() [] >>> bookmark = Bookmark( ... title=u'Mike Hall website', ... user=user1, ... link=link1 ... ) >>> bookmark.save() >>> user1.bookmark_set.all() [<Bookmark: Bookmark object>]
So, what just happened: we opened the “django database shell”, created two users, grabbed the 2nd user, grabbed the 1st link (we created that in our previous part), then we created a new Bookmark (cross-reference or many-to-many table) linking the user to the link.
So, now we can go to the browser and check on the user page for mdhall:
Big fun! Next time we’ll get into user registration and management.