I'm dabbling with generic views (thanks to wise advice from others on this list), and I'm trying to convert one of my slightly more complicated views to use a generic view. The vie itself is a simple list, but my queryset is generated as follows:
What I'm trying to achieve here is not locking users into a certain URL scheme - for the feedtype, I want uses to be able to specify '/link', '/links', '/LINKS', and any combination thereof, hence the regular expression which removes the trailing 's' from the request and performs the case-insensitive query. This works fine when I use it in a traditional view.
Now that I'm trying to use generic views (to avoid doing some ugly pagination by hand), my urls.py line is as follows:
NameError at /photo/ name 'feedtype' is not defined Request Method: GET Request URL: http://localhost:8000/photo/ Exception Type: NameError Exception Value: name 'feedtype' is not defined
I'm guessing that this is being caused by the regular expression evaluation not seeing that 'feedtype' exists - is there a way to solve this or will I have to use a standard view for this particular action?
> I'm dabbling with generic views (thanks to wise advice from others on > this list), and I'm trying to convert one of my slightly more > complicated views to use a generic view. The vie itself is a simple > list, but my queryset is generated as follows:
> What I'm trying to achieve here is not locking users into a certain > URL scheme - for the feedtype, I want uses to be able to specify > '/link', '/links', '/LINKS', and any combination thereof, hence the > regular expression which removes the trailing 's' from the request and > performs the case-insensitive query. This works fine when I use it in > a traditional view.
> Now that I'm trying to use generic views (to avoid doing some ugly > pagination by hand), my urls.py line is as follows:
> NameError at /photo/ > name 'feedtype' is not defined > Request Method: GET > Request URL: http://localhost:8000/photo/ > Exception Type: NameError > Exception Value: name 'feedtype' is not defined
> I'm guessing that this is being caused by the regular expression > evaluation not seeing that 'feedtype' exists - is there a way to solve > this or will I have to use a standard view for this particular action?
Do you really need the RE in the query? I think the problem is that the re call is being evaluated immediately, which the query is lazily evaluated. Would the "iexact" not work without the regular expression?
If you do end up needing the RE, you could try using a custom manager on your feeds model. Specifically, you could create a custom manager method which filters based on the regular expression, then pass that the captured feedtype from the URL.
On 15/08/06, Michael van der Westhuizen <r1mi...@gmail.com> wrote:
> Do you really need the RE in the query? I think the problem is that > the re call is being evaluated immediately, which the query is lazily > evaluated. Would the "iexact" not work without the regular expression?
> If you do end up needing the RE, you could try using a custom manager > on your feeds model. Specifically, you could create a custom manager > method which filters based on the regular expression, then pass that > the captured feedtype from the URL.
And oddly enough I'm getting exactly the same error; is there another problem with this statement which I'm not seeing? It seems there is a problem with variable from the url expression being passed to the view function - is this a common problem?
> And oddly enough I'm getting exactly the same error; is there another > problem with this statement which I'm not seeing? It seems there is a > problem with variable from the url expression being passed to the view > function - is this a common problem?
I think you've just identified the problem! Unfortunately, this means that you're going to need a custom view for this object list.
What's lacking in the generic object list is a "slugfield" similar to what's found in generic object detail. If that existed, you'd be able to do something like this:
I think it's worth raising a feature request in the Django Trac for this functionality. I can imagine that this would be very useful to a lot of people in the future.
> And oddly enough I'm getting exactly the same error; is there another > problem with this statement which I'm not seeing? It seems there is a > problem with variable from the url expression being passed to the view > function - is this a common problem?
You're using a variable "feedtype" that's not defined.
It's the same thing as doing this in the Python interactive prompt:
print a + 1
The variable "a" is not defined yet, so Python raises a NameError.
The problem in your case is that your view won't know the value of feedtype until the URL is parsed. For that reason, you cannot put that logic in the URLconf.
Adrian
-- Adrian Holovaty holovaty.com | djangoproject.com
> I think it's worth raising a feature request in the Django Trac for > this functionality. I can imagine that this would be very useful to a > lot of people in the future.
> You're using a variable "feedtype" that's not defined.
> It's the same thing as doing this in the Python interactive prompt:
> print a + 1
> The variable "a" is not defined yet, so Python raises a NameError.
> The problem in your case is that your view won't know the value of > feedtype until the URL is parsed. For that reason, you cannot put that > logic in the URLconf.
> Adrian
> -- > Adrian Holovaty > holovaty.com | djangoproject.com
Adrian,
I've just read your reply _after_ I submitted the ticket - please do close it if necessary.
Just out of interest, what do you think the most elegant way to solve this problem would be? Is it to write my own view and deal with the pagination myself?