On 4/5/06, PythonistL <[EMAIL PROTECTED]> wrote:
>
> Limodou,
> Thanks for replies.
> I am also working on a application that more users should be used by.
> So, I would like to learn a little more how to create
> url dynamicly( on fly). Can you please let me know where in you
> file(s) you make that.
> Thanks a lot
> L.
>
First you should define url pattern in urls.py, just like:
in ursl.py you will find:
(r'^blog/(?P<user_name>.*?)/', include('apps.woodlog.urls')),
and in apps/woodlog/urls.py you will find:
urlpatterns = patterns('',
(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<object_id>\d+?).html$',
'apps.woodlog.views.detail'),
(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$',
'apps.woodlog.views.day'),
(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/$', 'apps.woodlog.views.month'),
(r'^/?$', 'apps.woodlog.views.index'),
(r'^categories/(?P<category>\w+)/?$', 'apps.woodlog.views.view_category'),
(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<object_id>\d+?).html/addcomment/$',
'apps.woodlog.views.add_comment'),
)
So from two level url parsing, you will get 'username' and other
parameter. And you can process username in your view code, just like:
def index(request, user_name):
try:
user = User.objects.get(username=user_name)
except ObjectDoesNotExist:
return Http404(), "Doesn't exist this user!"
objects = user.entry_set.entries()
page = Page(request, objects, paginate_by=__get_paginate_by(user),
urlprefix=get_app_root('blog') + '/' + user_name + '/')
context = RequestContext(request, {'page':page, 'blog_id':user_name})
return theme_render_to_response('woodlog/list', user,
context_instance=context)
So the user is retrieved from User model according to username. And
later, I pass the user_name as 'blog_id' in RequestContext, so I can
use the 'blog_id' variable in template.
In templte just like woodlog/list, you can use 'blog_id' to create your url:
<a href="/blog/{{ blog_id }}">{{ blog_id }}</a>
That's all.
--
I like python!
My Blog: http://www.donews.net/limodou
My Django Site: http://www.djangocn.org
NewEdit Maillist: http://groups.google.com/group/NewEdit
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---