Re: Problem with URL naming, generic templates and custom filtering wrappers
Hi Karen, On Oct 10, 5:58 pm, Karen Traceywrote: > On Sat, Oct 10, 2009 at 6:34 PM, Scott SA wrote: > > I am trying to use a series of generic views with custom filtering via > > callbacks as per: > I cannot figure out how this one differs from the next one, nor can I > recreate first two errors you are getting (I just get the no reverse match), > and in making further changes you seem to be moving further away from what > you want to achieve, so I'm just going to stop here and fix this one. Sweet, thank you. 8-) > First, the "P?" should be "?P". You've got the order of the ? > and the P reversed. Finger dyslexia, happens all teh time! Was, and remains, correct in the urls file. I fabricated the stripped- down example to try to get to the heart of the matter without excessive detail. > Second, since you are specifying this as a bare tuple and not using url(), I tried using url() and others after reading the 'url' and 'patterns' source. > and you want to specify the optional name as 'url-pattern-name', you must > also include the optional dictionary which comes before the optional name as > defined here: > > http://docs.djangoproject.com/en/dev/topics/http/urls/#patterns > Given what you have 'url-pattern-name' is being taken to be the optional > dictionary, not the optional name. So change it to: > > (r'path/(?P[\w\d-]+)/?$', my_filter_callback, {}, > 'url-pattern-name'), Hmm, I thought I had done that but maybe something else was also wrong causing me to follow the wrong path for troubleshooting. > or use url() (read down further in the doc pointed to), omit the dictionary, > and specify the name via keyword: > > url(r'path/(?P[\w\d-]+)/?$', my_filter_callback, > name='url-pattern-name'), Okay, I know for sure that I tried this, may still have that code commented out. This is where I received the error that the 'name' attribute was being passed to 'my_filter_callback', as I recall. I'll give this another try just to be sure it wasn't a "red herring" that sucked a few hours out of my day. After having gone back and adding the empty dictionary in the patterns (prefix, tuple...) call, I now get resolution and reverse for that URL. The second part of my prev. statement re. the url call seems the likely culprit, and a quick double-check on the url() call just proved it. The problem is another reverse url call later in the page template, similar name, different regex, etc. 8-( Thanks for the quick and very informative reply, greatly appreciated. Sorry for the 'red herring' issue! Scott --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Problem with URL naming, generic templates and custom filtering wrappers
On Sat, Oct 10, 2009 at 6:34 PM, Scott SAwrote: > > Hi, > > I am trying to use a series of generic views with custom filtering via > callbacks as per: > > > http://docs.djangoproject.com/en/dev/topics/generic-views/#extending-generic-views > > Unfortunately, when I try to name the URL in urls.py, I get various > errors depending upon how I try to write the pattern call. > > I'll write a skeleton of what the code really is: > ___ > > in views.py: > - - - - - - - - - - - - - - > def my_filter_callback(request, name): > ... > return list_detail.object_list( request, queryset... ) > ___ > > in my_template.html: > - - - - - - - - - - - - - - > {% for object in obects_list %} > > {{ object.name }} > {% endfor %} > ___ > > in urls.py: > - - - - - - - - - - - - - - > from views import my_filter_callback > > urlpatterns += patterns( '', >( r'path/(P?[\w\d-]+)/?$', my_filter_callback, 'url-pattern- > name'), > ) > > I cannot figure out how this one differs from the next one, nor can I recreate first two errors you are getting (I just get the no reverse match), and in making further changes you seem to be moving further away from what you want to achieve, so I'm just going to stop here and fix this one. First, the "P?" should be "?P". You've got the order of the ? and the P reversed. Second, since you are specifying this as a bare tuple and not using url(), and you want to specify the optional name as 'url-pattern-name', you must also include the optional dictionary which comes before the optional name as defined here: http://docs.djangoproject.com/en/dev/topics/http/urls/#patterns Given what you have 'url-pattern-name' is being taken to be the optional dictionary, not the optional name. So change it to: (r'path/(?P[\w\d-]+)/?$', my_filter_callback, {}, 'url-pattern-name'), or use url() (read down further in the doc pointed to), omit the dictionary, and specify the name via keyword: url(r'path/(?P[\w\d-]+)/?$', my_filter_callback, name='url-pattern-name'), Karen --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Problem with URL naming, generic templates and custom filtering wrappers
Hi, I am trying to use a series of generic views with custom filtering via callbacks as per: http://docs.djangoproject.com/en/dev/topics/generic-views/#extending-generic-views Unfortunately, when I try to name the URL in urls.py, I get various errors depending upon how I try to write the pattern call. I'll write a skeleton of what the code really is: ___ in views.py: - - - - - - - - - - - - - - def my_filter_callback(request, name): ... return list_detail.object_list( request, queryset... ) ___ in my_template.html: - - - - - - - - - - - - - - {% for object in obects_list %} {{ object.name }} {% endfor %} ___ in urls.py: - - - - - - - - - - - - - - from views import my_filter_callback urlpatterns += patterns( '', ( r'path/(P?[\w\d-]+)/?$', my_filter_callback, 'url-pattern- name'), ) - - - - - - - - - - - - - - AttributeErrror: 'str' object has no attribute 'resolve' /opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/site-packages/django/core/urlresolvers.py in resolve 211. def resolve(self, path): 212. tried = [] 213. match = self.regex.search(path) 214. if match: 215. new_path = path[match.end():] 216. for pattern in self.url_patterns: 217. try: 218. sub_match = pattern.resolve(new_path) ___ in urls.py: - - - - - - - - - - - - - - from views import my_filter_callback urlpatterns += patterns( '', ( r'path/(P?[\w\d-]+)/?$', my_filter_callback, 'url-pattern- name'), ) - - - - - - - - - - - - - - ValueError at /path/ dictionary update sequence element #0 has length 1; 2 is required /opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/site-packages/django/core/urlresolvers.py in resolve 116. # positional arguments. 117. kwargs = match.groupdict() 118. if kwargs: 119. args = () 120. else: 121. args = match.groups() 122. # In both cases, pass any extra_kwargs as **kwargs. 123. kwargs.update(self.default_args) ___ Alternate version of urls.py urlpatterns += patterns( '', ( r'path/(P?[\w\d-]+)/?$', my_filter_callback ), ) # note: after reading the code, I also passed the prefix "myapp.views", # then called my callback as "my_filter_callback", trying to get # the 'url' method of /django/conf/urls/defaults.py to compose # the callback path as a string. - - - - - - - - - - - - - - TemplateSyntaxError at /path/ Caught an exception while rendering: Reverse for 'url-pattern-name' with arguments '()' and keyword arguments '{'name': u'some-name'}' not found. ___ So, I followed the source and tried a few different things. For example, I have tried to call RegexURLPattern adding a 'name' parameter, but that then gets passed to my callback, which in turn gets passed to django's generic list architecture. That subsequently generates an "unexpected attribute" error named "name", instead of it being used by RegexURLPattern to name the url pattern for reverse. Am I going to have to sub-class RegexURLPattern and force it to stick the name 'somewhere' appropriate, because at this point I don't think things are working as they should and I think I've dug deep enough to see that I've been making the correct calls. So, in a word: Help! Thanks in advance to any and all that can help me decode this mystery. Scott PS. My other alternative is to not reverse the URL, but calculate it m'self, which is less flexible. It may, in the short term be substantially quicker than some of my other alternatives. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---