Questions about HttpResponseRedirect and reverse
Hello, I was reading this discussion: http://groups.google.com/group/django-users/browse_thread/thread/d2d9ed6599089301 There Donn asks: "Lastly, on the decoupling thing again, if I end up using (sometime) HttpResposeRedirect(reverse(project.app.view)) does that not tie the view to the app to the project?" And Malcolm mentions: "Yes, but you don't have to use that way of referencing things. The safest way to do this (in terms of "it will always work not matter how you import things, etc") is to use the url(...) form for url patterns and use the fourth ("name") parameter to give the pattern a unique name. " I'm not sure if I understand Malcolm's suggestion, is he talking about using named URL patterns as documented here? http://docs.djangoproject.com/en/dev//topics/http/urls/#id2 Thanks for your help :) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Questions about HttpResponseRedirect and reverse
Cool, thanks for the confirmation. I'm still devouring all the documentation, a couple of books and practicing a lot on Django :) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Questions about HttpResponseRedirect and reverse
Alexis Bellido wrote: > Cool, thanks for the confirmation. I'm still devouring all the > documentation, a couple of books and practicing a lot on Django :) > One of the things I find tricksiest about learning new Django code is that the connections between the URLs and the views can often turn out to be a maze of twisty little passages, all alike. It's difficult (for me, anyway - maybe there's some obvious technique I have overlooked) to trace which pieces of code and which URLs are associated. regards Steve --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Questions about HttpResponseRedirect and reverse
On Friday, 03 October 2008 16:53:02 Alexis Bellido wrote: > I'm not sure if I understand Malcolm's suggestion, is he talking about > using named URL patterns as documented here? Yes he is. They are quite tricky little devils and cause me much pain, but that's because I am too impatient and have a memory like a big hole :) \d --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Questions about HttpResponseRedirect and reverse
On Fri, 2008-10-03 at 13:29 -0400, Steve Holden wrote: > Alexis Bellido wrote: > > Cool, thanks for the confirmation. I'm still devouring all the > > documentation, a couple of books and practicing a lot on Django :) > > > One of the things I find tricksiest about learning new Django code is > that the connections between the URLs and the views can often turn out > to be a maze of twisty little passages, all alike. It's difficult (for > me, anyway - maybe there's some obvious technique I have overlooked) to > trace which pieces of code and which URLs are associated. Hmm... this is getting off-topic a bit for this thread, but can you elaborate a bit more on what you mean here? I can't say this was something that every seemed tricky to me with Django, but I know different people learn and think differently, so it would be interesting to hear where there hidden shoals might be. It feels like it should be as simple as being a reg-exp pattern that captures some parameters and calls a view passing in those parameters and possible some extra ones. You can also give each pattern a name. I'm not mocking here. I'd like to hear how this can be hard so that I can at least experiment with some alternative explanations in the future when talking to people. Regards, Malcolm --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Questions about HttpResponseRedirect and reverse
Hey, Malcolm, maybe an example could help. I'm reading the forms documentation at http://docs.djangoproject.com/en/dev//topics/forms/ and I see this code as part of the view function for a contact form: def contact(request): if request.method == 'POST': # If the form has been submitted... form = ContactForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data # ... return HttpResponseRedirect('/thanks/') # Redirect after POST else: form = ContactForm() # An unbound form return render_to_response('contact.html', { 'form': form, }) I'd like to know the best way to use this line: return HttpResponseRedirect('/thanks/') # Redirect after POST that's a redirect to an absolute, hard-coded, url: /thanks. I guess that's not too portable, for example if I want to test at http://development-server.com/subdirectory before going to http://production-server.com/. How should I setup my URLConf and use HttpResposeRedirect and reverse in my view to be able to move my code independently of the domain or directory structure? Thanks! --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Questions about HttpResponseRedirect and reverse
After some more reading and testing I'll try to answer myself, if any of you could confirm if I'm on the right track please let me know and please correct me if I'm not using the right terminology as well: First, in my URLConf I have a url pattern like: (r'^search/$', 'books.views.search', {}, 'search_page'), I'm using a name url pattern here: 'search_page', that's the fourth parameter and I've included an empty dictionary as third parameter, can I just omit it? Then at the revelant place in my view function I have: return HttpResponseRedirect(reverse('search_page')) It works for me, am I doing it correctly? --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Questions about HttpResponseRedirect and reverse
On Sun, 2008-10-05 at 16:03 -0700, Alexis Bellido wrote: > After some more reading and testing I'll try to answer myself, if any > of you could confirm if I'm on the right track please let me know and > please correct me if I'm not using the right terminology as well: > > First, in my URLConf I have a url pattern like: > > (r'^search/$', 'books.views.search', {}, 'search_page'), > > I'm using a name url pattern here: 'search_page', that's the fourth > parameter and I've included an empty dictionary as third parameter, > can I just omit it? If you just omit it, the fourth parameter will then become the third parameter and there will be no fourth parameter. That isn't what you want. The best way to do this in "short form" is to use the url(...) style of url patterns. To wit: url(r'^search/$', 'books.views.search', name='search_page'), There's no real difference between this and the tuple form (internally, Django converts the tuple form into a url(...) form), but the above version is a proper Python function call, so you can use keyword arguments. The fourth argument is called "name", so you can specify it without needing to give the third argument. > > Then at the revelant place in my view function I have: > > return HttpResponseRedirect(reverse('search_page')) > > It works for me, am I doing it correctly? That is indeed correct. If you had any arguments in the search pattern, say, url(r'^search2/(\d+)/$', 'books.view.search', name='search_page2'), you could write reverse('search_page2', args=[42]) to reverse that call. The reverse() call takes "args" and "kwargs" parameters and the former should be a list or a tuple (the kwargs parameters would be a dictionary). You must call them "args" and "kwargs" and not use positional parameters, since, for historical reasons, the second argument of reverse() is something else that you don't use here. Regards, Malcolm --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Questions about HttpResponseRedirect and reverse
Cool, now it's completely clear. Thanks a lot Malcolm :) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Questions about HttpResponseRedirect and reverse
Malcolm Tredinnick wrote: > On Fri, 2008-10-03 at 13:29 -0400, Steve Holden wrote: > >> Alexis Bellido wrote: >> >>> Cool, thanks for the confirmation. I'm still devouring all the >>> documentation, a couple of books and practicing a lot on Django :) >>> >>> >> One of the things I find tricksiest about learning new Django code is >> that the connections between the URLs and the views can often turn out >> to be a maze of twisty little passages, all alike. It's difficult (for >> me, anyway - maybe there's some obvious technique I have overlooked) to >> trace which pieces of code and which URLs are associated. >> > > Hmm... this is getting off-topic a bit for this thread, but can you > elaborate a bit more on what you mean here? I can't say this was > something that every seemed tricky to me with Django, but I know > different people learn and think differently, so it would be interesting > to hear where there hidden shoals might be. > > It feels like it should be as simple as being a reg-exp pattern that > captures some parameters and calls a view passing in those parameters > and possible some extra ones. You can also give each pattern a name. > > I'm not mocking here. I'd like to hear how this can be hard so that I > can at least experiment with some alternative explanations in the future > when talking to people. I'll do my best to explain, but it's a little difficult to "go back and do it again" once the issues are past. It may be that I am complaining about the specific apps I have dealt with rather than Django generally. Or it may just be that there's something fundamental I've overlooked. I accept that you aren't mocking, but I know you will appreciate that it's difficult to confess ignorance at this apparently fundamental level when you've been working with the web for some while. I have just spent some time looking at how I can add user profiles to a test site, and decided this would best be done by adding the django-registration and django-profiles apps to it. Getting the code in was relatively easy -- about the only thing I had to do was fix up a newforms import. However, since I couldn't find much generic advice on gluing these bits and pieces together I am quite prepared to accept that it may just be because I've botched the job. I think from memory the most difficult issue was with the named URLs, as there is no definitive mapping between the textual names, the URL components and the names. (I realize that the names have been made close to the names of the views they map to, but knowing where to find them does require some familiarity with the code). Further, common URL prefixes don't necessarily map to views in the same module (though this could be my misconfiguration). I have attached (to avoid wrapping issues) a text file showing the various URLconfs that are (or should be) configured into my application. It's ended up as a bit of a dog's breakfast, having been patched together in a less-than-optimal way. What I was hoping for was to be able to generate some clear state transition diagram showing the sequences of URLs that particular actions like registering a new user, adding a profile and editing it, and so on would go through, along with the templates I would have to prepare and the available contexts for each template. So I suppose it's not really the individual mappings I find troublesome as much as the overall task flow. *Is* this just me? regards Steve --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~--- BASE SITE (r'^$', homepage), (r'^(students/.*)/$', stdpage), (r'^(py)/$', stdpage), (r'^(py/.*)/$', stdpage), (r'^(partners)/', stdpage), (r'^(partners/.*)/$', stdpage), (r'^(review/.*)$', stdpage), (r'^(opensource)/$', stdpage), (r'^(projects)/$', stdpage), (r'^(thankyou)/$', stdpage), (r'^(family)/$', stdpage), (r'^(training)/$', stdpage), (r'^(about)/$', stdpage), (r'^(search)/$', stdpage), (r'^registration/', include("registration.urls")), (r'^grp/', include('grp.urls')), (r'^(contact)', include('contact.urls')), url(r'^accounts/register/$', registration.views.register, {"profile_callback": hwprofile.models.create_profile}, name='registration_register'), # Non-overridden register package (r'^accounts/', include('registration.urls')), (r'^accounts/profile/', 'hwprofile.views.postlogin'), (r'^accounts/password/reset/complete/$', django.contrib.auth.views.password_reset_complete, {}, 'password_reset_complete'), (r'^accounts/password/reset/confirm/(?P.+)/(?P\d)/$', dja