Syllabus 2010 t3 p2

From XPUB & Lens-Based wiki

The assignment is to create a simple "overview" or list view of your collection.

Due 15 Jun


Example

The following is a simple example from class of a Music viewing application.

First, here's a simple single model (Track), using a text field with choices for giving each track a rating.

models.py

models.py (inside the "application" folder)

from django.db import models

RATINGS = (
    ('1', '*'),
    ('2', '**'),
    ('3', '***'),
    ('4', '****'),
    ('5', '*****')
)

class Track (models.Model):
    file = models.FileField(upload_to="audio")
    rating = models.CharField (max_length=255, choices=RATINGS, blank=True)
    email = models.EmailField(blank=True)
    
    def __unicode__ (self):
        return self.file.name


urls.py

Write a url pattern that includes an item id:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^music/$', 'music.views.track_list'),
    (r'^music/(?P<id>\d+)$', 'music.views.track_detail'),
)


views.py

Also within your application folder.

from django.shortcuts import render_to_response
from django.template import RequestContext
from models import *

def track_list (request):
    context = {}
    context['tracks'] = Track.objects.all()
    return render_to_response("track_list.html", context, context_instance = RequestContext(request))


Then make a view to see the details of a specifc track:

def track_detail (request, id):
    context = {}
    context['track'] = Track.objects.get(id=id)
    return render_to_response("track_detail.html", context, context_instance = RequestContext(request))

template

<h1>cool audio player</h1>
<img src="" />
<table border="1">
<tr>
<td>Name of track</td>
<td>Rating</td>
</tr>

{% for track in tracks %}
<tr>
<td>
    <a href="">     
        {{ track.file.name }}
    </a>
</td>
<td>{{ track.rating }}</td>
</tr>
{% endfor %}

</table>


urls.py

Change "urls.py" to include a mapping from a URL to a view function in your applications "view.py". At the "project" level -- next to "settings.py", you can eventually also create a urls.py inside your application folder and include it from the "outer" urls.py for increased modularity)

from django.conf.urls.defaults import *

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^music/', "music.views.track_list"),
    (r'^admin/', include(admin.site.urls)),
)


Albert:

Syllabus 2010 t3 p2 face albert.png
from django.db import models
from sorl.thumbnail.fields import ImageWithThumbnailsField

class Poster(models.Model):
    title = models.TextField(blank=True)
    body = models.TextField(blank=True)
    # images = []
    def __unicode__(self):
        return self.title 

parts = (
("e", "eye"),
("m", "mouth"),
("n", "nose"),
("h", "hand"),
)

class Image(models.Model):
    image = ImageWithThumbnailsField(
        upload_to='images',
        thumbnail={'size': (120, 120)},
        extra_thumbnails={
            'icon': {'size': (32, 32), 'options': ['crop', 'upscale']},
            'large': {'size': (640, 640)},
        }
    )
    part = models.CharField(max_length=255, choices=parts)
    #poster = models.ManyToManyField(Poster, related_name="images")
    
    def __unicode__(self):
        return self.image.name


Attachments

  • Syllabus 2010 t3 p2 face albert.png