Wednesday, December 7, 2011

Re: URL Issue - Slugged Url with Custom IDs

I found an article that described how theonion.com did something
similar. Their url is the slug and an ID separated by a comma. The
article said pulling by ID gives a performance gain. But they wanted
to keep the seo friendly url as well.

http://www.theonion.com/articles/secretary-of-agriculture-attends-diplomatic-meetin,26822/

With that idea I implemented a solution that I like:

My path resolves to: /<slug>,<api_id>/foo

#urls.py
urlpatterns = patterns('app.views',
(r'^(?P<pageslug>[\w-]+),(?P<api_id>.*)/foo/$', 'viewfunction'),
)

#views.py
def viewfunction(request, api_id, pageslug):
if pageslug != themodel.slug:
return
HttpResponsePermanentRedirect(thebattle.get_absolute_url())
else:
themodel = get_object_or_404(MyModel, custom_id=api_id)
...

#models.py
set get_absolute_url() in models.py accordingly.

~Matt

On Dec 7, 1:36 pm, mattym <slackbabb...@gmail.com> wrote:
> Hi all -
>
> Question. I would love to use a slug in the usual way for nice urls:
>
> #urls.py
> urlpatterns = patterns('app.views',
>     (r'^(?P<myslug>.*)/foo/$', 'viewfunction'),
> )
>
> #views.py
> def viewfunction(request, myslug):
>     themodel = get_object_or_404(MyModel, slug=myslug)
>
> However, due to requirements 'themodel' will have to be pulled by a
> custom id that is retrieved via an API.  From there to get proper
> routing I had to chang urls.py and views.py to.
>
> #urls.py
> urlpatterns = patterns('app.views',
>     (r'^(?P<api_id>.*)/foo/$', 'viewfunction'),
> )
>
> #views.py
> def viewfunction(request, api_id):
>     themodel = get_object_or_404(MyModel, custom_id=api_id)
>
> This works. But with ugly URLS:
>
> What is the cleanest way to pull mymodel with the custom api_id, and
> still have a slug/seo friendly url?
>
> This seems simple but my mind is trying to make it complex.
>
> Thanks,
> Matt

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate