Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » URLConf/ManytoManyField issue (exercise problem from Teach Yourself Django in 24 Hour) [RSS Feed]

#1 March 8, 2008 02:43:33

Rilindo F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

URLConf/ManytoManyField issue (exercise problem from Teach Yourself Django in 24 Hour)


Hi!

I am going Django (liking it so far), and I seem to hit a wall with
one of the exercises. It likely because of my unfamiliarity with
Python, so bear with me, please.

At the end of Hour 6, I got two exercises to do:

1. Create an additional view for the People object that displays the
Blog contents for a specific Person.
2. Create an URL pattern in the URLConf that collects the Person's ID
from the URL request and passes it to the view function that you
created in Exercise .

So this is the code I have so far:

url.py:

from django.conf.urls.defaults import *

details1 = {'opts':('name','email')}
details2 = {'opts':('name','birthday')}
details3 = {'opts':('name','desc','favoriteURL')}
details4 = {'opts':('name','blogs')}

urlpatterns = patterns('iFriends.People.views',
(r'^$','index'),
(r'^Info/$','details'),
(r'^Info/(?P<pID>\d+)/$', 'details'),
(r'^Contact/(?P<pID>\d+)/$','details', details1),
(r'^Birthday/(?P<pID>\d+)/$','details', details2),
(r'^Details/(?P<pID>\d+)/$','details', details3),
(r'^Blog/(?P<pID>\d+)/$','blog_details', details4), # This is the one
I have problems with


)

The view that I created:


def blog_details(request, pID='0', opts=()):

response = HttpResponse()
response.write("<HTML><BODY>\n")
try:
b = Person.objects.get(id=pID)
for d in opts:
response.write("<li>%s: %s</li>" % (d,
b.__dict__))
except Blog.DoesNotExist:
response.write("Blog Not Found")

response.write("</BODY></HTML>")
return response


These are the models that I am working with:

from django.db import models
from django.contrib.auth.models import User

gender_list = (('M','Male'), ('F', 'Female'))
# Create your models here.


class Blog(models.Model):
title = models.CharField('Title', maxlength=200)
text = models.TextField('Text', maxlength=2048)

class Admin:
pass

class Person(models.Model):
userID = models.ForeignKey(User, unique=True)
name = models.CharField('name', maxlength=200)
birthday = models.DateField('Birthday',blank=True,null=True)
gender = models.CharField(maxlength=1, choices=gender_list)
email = models.EmailField('Email', unique=True)
headshot = models.ImageField(upload_to='img', blank=True)
favoriteURL = models.URLField('myURL')
favoriteBooks = models.ManyToManyField('self',blank=True)
favoriteMovies = models.ManyToManyField('self',blank=True)
desc = models.TextField('Desc',maxlength=500, null=True)
friends = models.ManyToManyField('self',blank=True)
blogs = models.ManyToManyField(Blog, blank=True)

def __str__(self):
return '%s' % (self.name)

class Admin:
pass


I am able to at least pass the parameters, that I know. That said, I
kept getting this:

AttributeError at /People/Blog/1/
'QuerySet' object has no attribute 'text'

Request Method: GET
Request URL:http://giggle.org:8000/People/Blog/1/Exception Type: AttributeError
Exception Value: 'QuerySet' object has no attribute 'text'
Exception Location: /home/rilindo/iFriends/../iFriends/People/views.py
in blog_details, line 53





It is possible that I don't have a clear understanding on how to use
the ManytoManyField, but I could be wrong. At any event, It is
probably something simple that I am missing.

Help?

- Rilindo
--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#2 March 8, 2008 12:04:22

Evert R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

URLConf/ManytoManyField issue (exercise problem from Teach Yourself Django in 24 Hour)


> I am going Django (liking it so far), and I seem to hit a wall with
> one of the exercises. It likely because of my unfamiliarity with
> Python, so bear with me, please.
>
> At the end of Hour 6, I got two exercises to do:
>
> 1. Create an additional view for the People object that displays the
> Blog contents for a specific Person.
> 2. Create an URL pattern in the URLConf that collects the Person's
> ID from the URL request and passes it to the view function that you
> created in Exercise .
>
> So this is the code I have so far:
>
> url.py:
>
> from django.conf.urls.defaults import *
>
> details1 = {'opts':('name','email')}
> details2 = {'opts':('name','birthday')}
> details3 = {'opts':('name','desc','favoriteURL')}
> details4 = {'opts':('name','blogs')}
>
> urlpatterns = patterns('iFriends.People.views',
> (r'^$','index'),
> (r'^Info/$','details'),
> (r'^Info/(?P<pID>\d+)/$', 'details'),
> (r'^Contact/(?P<pID>\d+)/$','details', details1),
> (r'^Birthday/(?P<pID>\d+)/$','details', details2),
> (r'^Details/(?P<pID>\d+)/$','details', details3),
> (r'^Blog/(?P<pID>\d+)/$','blog_details', details4), # This is the
> one I have problems with
>
>
> )
>
> The view that I created:
>
>
> def blog_details(request, pID='0', opts=()):
>
> response = HttpResponse()
> response.write("<HTML><BODY>\n")
> try:
> b = Person.objects.get(id=pID)
> for d in opts:
> response.write("<li>%s: %s</li>" % (d,
> b.__dict__))
> except Blog.DoesNotExist:

Have you checked what 'd' is here? It may be something different than
you'd expect or hope, in particular for the __dict__ lookup (not
sure what you're expecting here).
I would actually work with something like
Person.objects.filter(id=pID), which keeps b a QuerySet instead of
turning it into an object, and then do more filtering later on.

But, from the error message, I'm guessing that's actually not (yet)
your problem. It complains about the 'text' attribute that cannot be
found, which would suggest that that field doesn't exist in the actual
database (it's there in the model). Did you sync the database, and
then later added the text attribute to Blog (or even the whole Blog)?
Btw, where in your view is line 53? I'm guessing at "b =
Person.objects.get(id=pID)"?

It may be that you need recreate the database part for this app. One
way to do this is to just to simply move the current database aside,
and run python manage.py syncdb again. That gets rid of all current
entries though.
You could also try to output the new sql (python manage.py sql <app>),
and see what you'll need to add in the database directly.

Good luck

> response.write("Blog Not Found")
>
> response.write("</BODY></HTML>")
> return response
>
>
> These are the models that I am working with:
>
> from django.db import models
> from django.contrib.auth.models import User
>
> gender_list = (('M','Male'), ('F', 'Female'))
> # Create your models here.
>
>
> class Blog(models.Model):
> title = models.CharField('Title', maxlength=200)
> text = models.TextField('Text', maxlength=2048)
>
> class Admin:
> pass
>
> class Person(models.Model):
> userID = models.ForeignKey(User, unique=True)
> name = models.CharField('name', maxlength=200)
> birthday = models.DateField('Birthday',blank=True,null=True)
> gender = models.CharField(maxlength=1, choices=gender_list)
> email = models.EmailField('Email', unique=True)
> headshot = models.ImageField(upload_to='img', blank=True)
> favoriteURL = models.URLField('myURL')
> favoriteBooks = models.ManyToManyField('self',blank=True)
> favoriteMovies = models.ManyToManyField('self',blank=True)
> desc = models.TextField('Desc',maxlength=500, null=True)
> friends = models.ManyToManyField('self',blank=True)
> blogs = models.ManyToManyField(Blog, blank=True)
>
> def __str__(self):
> return '%s' % (self.name)
>
> class Admin:
> pass
>
>
> I am able to at least pass the parameters, that I know. That said, I
> kept getting this:
>
> AttributeError at /People/Blog/1/'QuerySet' object has no attribute
> 'text'
> Request Method:
> GET
> Request URL:
>http://giggle.org:8000/People/Blog/1/> Exception Type:
> AttributeError
> Exception Value:
> 'QuerySet' object has no attribute 'text'
> Exception Location:
> /home/rilindo/iFriends/../iFriends/People/views.py in blog_details,
> line 53
>
>
>
>
>
> It is possible that I don't have a clear understanding on how to use
> the ManytoManyField, but I could be wrong. At any event, It is
> probably something simple that I am missing.
>
> Help?
>
> - Rilindo
>
> --~--~---------~--~----~------------~-------~--~----~
> 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
> For


--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#3 March 9, 2008 03:33:07

Rilindo F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

URLConf/ManytoManyField issue (exercise problem from Teach Yourself Django in 24 Hour)


On Mar 8, 2008, at 7:04 AM, Evert Rol wrote:
>
> Have you checked what 'd' is here? It may be something different than
> you'd expect or hope, in particular for the __dict__ lookup (not
> sure what you're expecting here).
> I would actually work with something like
> Person.objects.filter(id=pID), which keeps b a QuerySet instead of
> turning it into an object, and then do more filtering later on.


I can do that, but then I get a key error with the name. However, it
works with similar code here:

def details(request, pID='0', opts=()):
response = HttpResponse()
response.write("<HTML><BODY>\n")
try:
p = Person.objects.get(id=pID)
if (pID == '0'):
response.write("<H1>Person Details Index</H1><HR>\n")
else:

for d in opts:
response.write("<li>%s: %s</li>" % (d,
p.__dict__))
except Person.DoesNotExist:
response.write("Person Not Found")

response.write("</BODY></HTML>")
return response
>
>
> But, from the error message, I'm guessing that's actually not (yet)
> your problem. It complains about the 'text' attribute that cannot be
> found, which would suggest that that field doesn't exist in the actual
> database (it's there in the model). Did you sync the database, and
> then later added the text attribute to Blog (or even the whole Blog)?
> Btw, where in your view is line 53? I'm guessing at "b =
> Person.objects.get(id=pID)"?

I dropped the database and resynced it. No go.


I then went and changed the URL code to pass the params to the view
with this to this:

details4 = {'opts':('name', 'blogs')}

And now I get:

KeyError at /People/Blog/1/
'blogs'



Which confused me a bit, since blogs is also a key.

I can probably skip this excercise and proceed on with the rest of the
book since I know now how to pass multiple views and have multiple
URLs, but this concerns me. Part of me still doesn't understand fully
why it is not working and I get a feeling that if I don't resolve
this, I probably screw up the rest of the exercises in the book. :|
--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#4 March 9, 2008 03:36:36

Rilindo F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

URLConf/ManytoManyField issue (exercise problem from Teach Yourself Django in 24 Hour)


Oh yeah, SQL output:

:~/iFriends$ python manage.py sql People
BEGIN;
CREATE TABLE "People_blog" (
"id" serial NOT NULL PRIMARY KEY,
"title" varchar(200) NOT NULL,
"text" text NOT NULL
);
CREATE TABLE "People_person" (
"id" serial NOT NULL PRIMARY KEY,
"userID_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id")
DEFERRABLE INITIALLY DEFERRED,
"name" varchar(200) NOT NULL,
"birthday" date NULL,
"gender" varchar(1) NOT NULL,
"email" varchar(75) NOT NULL UNIQUE,
"headshot" varchar(100) NOT NULL,
"favoriteURL" varchar(200) NOT NULL,
"desc" text NULL
);
CREATE TABLE "People_person_favoriteBooks" (
"id" serial NOT NULL PRIMARY KEY,
"from_person_id" integer NOT NULL REFERENCES
"People_person" ("id") DEFERRABLE INITIALLY DEFERRED,
"to_person_id" integer NOT NULL REFERENCES "People_person" ("id")
DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("from_person_id", "to_person_id")
);
CREATE TABLE "People_person_favoriteMovies" (
"id" serial NOT NULL PRIMARY KEY,
"from_person_id" integer NOT NULL REFERENCES
"People_person" ("id") DEFERRABLE INITIALLY DEFERRED,
"to_person_id" integer NOT NULL REFERENCES "People_person" ("id")
DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("from_person_id", "to_person_id")
);
CREATE TABLE "People_person_friends" (
"id" serial NOT NULL PRIMARY KEY,
"from_person_id" integer NOT NULL REFERENCES
"People_person" ("id") DEFERRABLE INITIALLY DEFERRED,
"to_person_id" integer NOT NULL REFERENCES "People_person" ("id")
DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("from_person_id", "to_person_id")
);
CREATE TABLE "People_person_blogs" (
"id" serial NOT NULL PRIMARY KEY,
"person_id" integer NOT NULL REFERENCES "People_person" ("id")
DEFERRABLE INITIALLY DEFERRED,
"blog_id" integer NOT NULL REFERENCES "People_blog" ("id")
DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("person_id", "blog_id")
);


On Mar 8, 2008, at 10:32 PM, Rilindo Foster wrote:

>
> On Mar 8, 2008, at 7:04 AM, Evert Rol wrote:
>>
>> Have you checked what 'd' is here? It may be something different than
>> you'd expect or hope, in particular for the __dict__ lookup (not
>> sure what you're expecting here).
>> I would actually work with something like
>> Person.objects.filter(id=pID), which keeps b a QuerySet instead of
>> turning it into an object, and then do more filtering later on.
>
>
> I can do that, but then I get a key error with the name. However, it
> works with similar code here:
>
> def details(request, pID='0', opts=()):
> response = HttpResponse()
> response.write("<HTML><BODY>\n")
> try:
> p = Person.objects.get(id=pID)
> if (pID == '0'):
> response.write("<H1>Person Details Index</H1><HR>\n")
> else:
>
> for d in opts:
> response.write("<li>%s: %s</li>" % (d,
> p.__dict__))
> except Person.DoesNotExist:
> response.write("Person Not Found")
>
> response.write("</BODY></HTML>")
> return response
>>
>>
>> But, from the error message, I'm guessing that's actually not (yet)
>> your problem. It complains about the 'text' attribute that cannot be
>> found, which would suggest that that field doesn't exist in the
>> actual
>> database (it's there in the model). Did you sync the database, and
>> then later added the text attribute to Blog (or even the whole Blog)?
>> Btw, where in your view is line 53? I'm guessing at "b =
>> Person.objects.get(id=pID)"?
>
> I dropped the database and resynced it. No go.
>
>
> I then went and changed the URL code to pass the params to the view
> with this to this:
>
> details4 = {'opts':('name', 'blogs')}
>
> And now I get:
>
> KeyError at /People/Blog/1/
> 'blogs'
>
>
>
> Which confused me a bit, since blogs is also a key.
>
> I can probably skip this excercise and proceed on with the rest of
> the book since I know now how to pass multiple views and have
> multiple URLs, but this concerns me. Part of me still doesn't
> understand fully why it is not working and I get a feeling that if I
> don't resolve this, I probably screw up the rest of the exercises in
> the book. :|
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

  • Root
  • » Django
  • » URLConf/ManytoManyField issue (exercise problem from Teach Yourself Django in 24 Hour) [RSS Feed]

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support