:::: MENU ::::

The Project: Django Views

This is probably the most simple Django Views.py file ever. I’m just not there yet. I’ve been reading about using the generic views in your own views, but I haven’t implemented that yet. I’ve not posted the templates as I had mentioned in the last post. I’m still struggling with getting the value out of the podcast genre choices field. I can get the first element of the dictionary, but I want the second one. Actually just thinking about it now, there is probably a model function that needs to return the second element of the dictionary. I’ll probably have to write that myself. If you have a suggestion for that, leave a comment.

Here is the content of the uber simple Views.py:

from django.shortcuts import render_to_response
from podcasts.models import Podcast

def indexPage(request):
    return render_to_response('podcast/index.html',{})

I think it’s probably time to create some genre sorted views to go along with the time based list. That will be next. Meanwhile, seriously, if you have a suggestion about getting that choice element out into the template, leave a comment.

Be Sociable, Share!

4 Comments

  • Chris Pratt |

    If I understand you correctly, you’re trying to get the human-readable value from the choices list (i.e. the value you would see in the select list of the form), rather than the actual value that gets stored in the database.

    If that’s the case, the solution is frustratingly simple: get_FOO_display().

    Example:

    COLOR_CHOICES = (
    (0, ‘Red’),
    (1, ‘Green’),
    (2, ‘Blue’),
    )

    color = models.ChoiceField(choices=COLOR_CHOICES)

    instance.color
    >>> 1
    instance.get_color_display()
    >>> ‘Green’

    Hope that helps.

  • Brian Luft |

    Hi Russ,

    If you wouldn’t mind posting the relevant parts of your code to dpaste.com (yes it’s a Django App) I/we can help you out.

    -Brian

  • Russ Turley |

    Chris,

    Your suggestion worked. I also saw that bit of information in the Django Project documentation, but I couldn’t figure out how to get the template to display the choice, until I found this post:

    http://code.djangoproject.com/ticket/4023

    It explains that you use the {{object.get_FOO_display}} in your template instead of the {{object.FOO}}. I simply changed my template and bingo, it worked.

    Thanks for the help.
    Russ