On Aug 24, 7:38 am, rmschne <[EMAIL PROTECTED]> wrote: > I'm new to Django, and have played with Python for a few years on and > off (not a real programmer). I'm keen to use Django to explore some > data because of its custom data models which I plan to rely on > extensively (putting the complex code there). > > Try as I might, I can't see how to get them to work. I can get the > fields from the database to display in the html created by the > render_to_response() function, but the custom fields do not compute > and display. They come out empty. I'm also struggling how to get > access to the data inside of Python to enable data handling by other > Python code. For example, when I issue a "print mvalues[0]" in the > example below, I get all the fields for the first record of the > database returned, but I don't the custome fields, which I expected. > My guess is I have to some other code format, but I don't see examples > for this in "The Definitive Guide to django" by Holovaty and Kaplan- > Moss. > > Example: The data model- > > class Tmeetings(models.Model): > Date = models.DateTimeField(null=True, blank=True) > Speaker = models.CharField(blank=True, max_length=150) > Venue = models.CharField(blank=True, max_length=150) > Cost = models.FloatField(null=True, blank=True) > > def __str__(self): > return '%s, %s' % (self.Date, self.Speaker) > > def test (self): > return self.Speaker+self.Venue > > class Meta: > db_table = 'tmeetings' > > class Admin: pass > > Example: The test template file mtg_summary.html > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > "http://www.w3.org/TR/html4/loose.dtd"> > <html><head><title>{{ meeting_title }} </title><meta http- > equiv="Content-Type" content="text/html; charset=iso-8859-1" ></head> > <body> > > <p>template: mtg_summary.html</p> > <p>Talk_title: {{ Talk_Title }}</p> > <p>Speaker: {{ Speaker }}</p> > <p>Speaker_Title:{{ Speaker_title }}</p> > <p>test: {{ test }}</p> > > </body></html> > > Example: The Problem (code snippet) > > >>>from django.shortcuts import render_to_response > >>>from soc.models import Tmeeting > >>>m_values=Members.objects.values() > >>> print render_to_response('mtg_summary.html',mvalues[0]) > > Content-Type: text/html; charset=utf-8 > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > "http://www.w3.org/TR/html4/loose.dtd"> > <html> > <head> > <title> </title> > <meta http-equiv="Content-Type" content="text/html; > charset=iso-8859-1" > > </head> > <body> > <p>template: mtg_summamry.html</p> > <p>Talk_title: None</p> > <p>Speaker: George Smith</p> > <p>Speaker_Title: Title of the test record</p> > <p>test: </p> > </body> > </html> > >
It's not at all clear from the code what you're trying to do. talk_title and speaker_title aren't defined anywhere in the code you've given us, but I'm not even sure if those are the 'custom fields' you're referring to. What's more, the code you've given us would not be capable of producing the output you've shown, so this clearly isn't the actual code. We would need to see the actual view and template code - it's best if you paste it at dpaste.com and give us a link. If by 'custom fields' you mean methods on the model, like your test() example, you wouldn't expect the values() method to show them. It will only show the actual model fields. I think you're misunderstanding how to access models in your code and templates. values() isn't the normal way of doing it - really, you just want to be passing an instance of Tmeeting to your template and then using normal dot notation to access the values. For example: my_meeting = Tmeeting.objects.all()[0] print my_meeting.date print my_meeting.speaker print my_meeting.test() print render_to_response('mtg_summary.html', {'my_meeting':my_meeting}) and in the template: <p>Talk_title: {{ my_meeting.talk }}</p> <p>Speaker: {{ my_meeting.speaker }}</p> <p>Speaker_Title:{{ my_meeting.speaker_title }}</p> {# where is this coming from? #} <p>test: {{ my_meeting.test }}</p> etc. (Note that the convention is to have lower case names for model fields and methods.) -- DR. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---