On May 23, 2:33 pm, Bobby Roberts <tchend...@gmail.com> wrote:
> > The code you have given is incapable of producing that error - request
> > is passed as a parameter, so will always be defined within the
> > function. Are you sure the error is coming from within the function?
> > Give us the actual traceback that is being produced.
> > --
> > DR.
>
> def GetRequestId(request):
>     curpath=request.path
>     return curpath
>
> # setup a subclass for URL on add form which will be used by the ADD
> and CLONE views.
> # the purpose is ti screen for urls goign into the system (non-preview
> urls) and
> class ExistingUrl (forms.Field):
>     def clean(self, value):
>
>         i=GetRequestId(request)          <<<<<< error is here

Yes, the error is here, not in the GetRequestId function.
Clearly, at this point there is no 'request' variable - this should be
obvious, as the function is called with the parameters (self, value)
and request is not one of them.

> Here's what i'm trying to do. I've got a module that allows us to add
> new items and edit new items to the database.  I was going to use a
> url slug which has to be unique anyway, but that will not work for
> this application.  As a result i'm allowing users to enter a url path
> (ie /whatever/this/page/is/) and it's being served up fine through a
> middleware class I wrote to substitute for flatpges (it does more than
> standard flatpages).  In my form above I'm trying to find a way to
> check and see on form submission if the url has been changed to
> another URL which is in the database already and not for the current
> record.  That part I can handle.  I think the key to this is getting
> request.path into my form field subclass above so that I can get the
> record id which is passed through the url.

Absolutely. So the standard solution is to override __init__ in your
form class to accept request as one of the kwargs, then stash it in an
attribute on the form. Then (and it'll be easier to do this in a
clean_fieldname() method on the form, rather than in a custom
formfield) you can access the self.request attribute as required.

class MyForm(forms.Form):
    def __init__(self, *args, **kwargs):
        request = kwargs.pop('request', None)
        if request:
            self.request = request
        super(MyForm, self).__init__(*args, **kwargs)

... and in the view:
myform = MyForm(request.POST, request=request)

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

Reply via email to