2009/3/6 Malcolm Tredinnick <[email protected]>
>
> On Thu, 2009-03-05 at 13:35 +0100, Dries Desmet wrote:
> > Hi all,
Hi Malcolm,
By reading your reply I realise I almost wrote a rumbling e-mail
without thinking too much, but bare with me, I feel that communicating
my thoughts is something of a learning curve itself.
>
> > I want to start a very simple tree-structure cms django app. Reading
> > about django all day yesterday, my plan is to have the urlconf capture
> > every and all urls by:
> > (r'', 'pages.views.index')
> >
> > and use the request.url object to differentiate between the pages by
> > checking them against a .url attribute of the pages model.
I meant: 'and use request.path to differentiate between the requested pages'
request is an object that gets send to the views anyway.
Be precise!
>
> > If request.path="/" I have to be at the index page, right?
>
> No. There could be a "script name" on the front of that. So if the
> webserver is configured to only pass everything under, say, "/foo/bar/"
> to Django, then a request for "/foo/bar/" will show appear to Django as
> being a request for "/".
Okay got it. I forgot that the server could be configured to only set
a subsection of the requests to django. Good point! I guess herein
lies the difference between capturing a django url and using a
HttpRequest.path object. I'm presuming the latter will actually return
the full url without the domain but including the path that wasn't
necessarily passed on to django.
>
>
> > So it 'll be something like
> > try:
> > pages.object.get(url=request.url)
> >
> > except:
> > return a 404
>
> Based on the complete lack of code you've provide about what pages exist
> and what the page model is, it's difficult to tell what's going on
> there, so I'll skip this.
2nd attempt (assuming I'm still using the urlconf form the start of
this e-mail (r'', 'pages.views.index'):
#in pages/models.py
from django.db import models
class Page(models):
url = models.CharField(max_length=200)
#in pages/views.py
from django.http import HttpResponse
from pages.models import Page
def index(request):
try:
p=Page.objects.get(request.path)
except Page.DoesNotExist:
raise Http404
return HttpRespones("You're looking at the page with url %s" % p)
A page in the database with url="/" will therefor be the index page. Right?
>
> >
> >
> > 1) Is it good practice to have a urlconf capture everything
>
> Only if you want to capture everything. That's pretty rare. Most
> projects have multiple URL patterns to match different things and send
> them to different views.
For a cms system I would want to capture everything as a last resort
because I can't foresee what urls will be made. Of course I need to
put the capture all after others such as /admin. Or are there
alternative approaches?
>
> > and is my regex of empty string waterproof?
>
> Your URLConf pattern will match *everything*, but capture nothing. Not
> just every request for '/'. If you want to match just '/', then do just
> that, with a pattern like:
>
> '/$'
>
> > 2) Is r'^.*$' any better?
>
> That will also match everything, except that it will consume it (and not
> capture anything, since you don't have any grouping parentheses), rather
> than simply matching the "nothing" at the start of the string.
As said I realise now I should probably capture the url, but I don't
understand the difference between 'consuming' and mathing here.
>
> > 3) Why do most examples use (?P<url>) to capture the url instead of
> > request.url that gets passed to the view anyway already?
>
> Because most patterns aren't trying to capture everything. That avoids
> the whole benefit of the URL Conf, section and simply passes exactly the
> same amount of work into the view. The URL Conf pattern is used to split
> things up into components so that view functions look like standard
> Python functions with a bunch of readable arguments passed in.
For a cms, I probably need to differentiate page requests in the view
and therefor capture everything except /admin.
>
> Oh, and by the way, request.url doesn't seem to exist, either, so I'm
> not sure what you're doing there. Certainly nothing in
> django/core/handlers/* assigns to that attribute and the string
> "request.url" doesn't appear anywhere in the source tree.
request.url doesn't exist, but from the docs:
When a page is requested, Django creates an HttpRequest object that
contains metadata about the request. Then Django loads the appropriate
view, passing the HttpRequest as the first argument to the view
function.
>
> Regards,
> Malcolm
Much appreciated,
Dries.
>
>
> >
--
Urga Digital FX,
Engelstraat 167
9040 Gent
www.urga.be
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---