Name Error in django class method

10 views
Skip to first unread message

Navaneethan R

unread,
Oct 28, 2011, 7:20:22 AM10/28/11
to django...@googlegroups.com
 

              The Django model I have defined a model called "File" and there i have writted a custom function which is used to split the filefield for my intention.while iterating the File object through this function i am getting "Name Error" for the file field

class File(models.Model):
     name
= models.CharField(max_length=200)
     file_upload
= models.FileField(upload_to=settings.FILEPATH,blank=True,null=True,max_length=1024)
     organisation
= models.ManyToManyField(Organisation)

     
def __unicode__(self):
         
return self.name
     
def public_url(self):
         
if os.path.exists(self.file_upload.path):
             
return '/site_media/'+str(self.file_upload.path).split('/site_media/')[1]

Trace back:

>>> File.objects.get(pk=1).public_url()
Traceback (most recent call last):
 
File "<console>", line 1, in <module>
 
File "/work/ws/intcen/gridlex_intelligence/intelligence_centre/models.py", line 52, in public_url
 
if os.path.exists(self.file_upload.path):
 
NameError: global name 'file_upload' is not defined


Andre Terra

unread,
Oct 28, 2011, 9:21:39 AM10/28/11
to django...@googlegroups.com
Have you imported the os module?


Cheers,
AT


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/EPrMTjuYE6wJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Andre Terra

unread,
Oct 28, 2011, 9:23:42 AM10/28/11
to django...@googlegroups.com
And sorry for the extra e-mail, but I just realized your code isn't abiding by PEP8 [1].

Please read through it and try to follow some of the basic conventions, such as adding spaces after commas, adding spaces around operators (such as +) and splitting lines so that they're not longer than 79 chars (72 chars for long comments and strings).


Cheers,
AT

[1] http://www.python.org/dev/peps/pep-0008/

Navaneethan R

unread,
Oct 30, 2011, 3:20:16 AM10/30/11
to django...@googlegroups.com
yes i had imported os module.

Navaneethan R

unread,
Oct 30, 2011, 3:20:42 AM10/30/11
to django...@googlegroups.com
yes-sure i ll follow the instructions note.
Reply all
Reply to author
Forward
0 new messages