Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I convert this long and complex PHP style URL query string to a Django url?

I'm converting a small PHP application to Django.

One section has a long query string, indicating how a widget is to be displayed. There are a few required parameters and several optional ones.

The current urls read like:

app.php?id=102030&size=large&auto=0&bw=1&extra=1

The id and size are required, but auto, bw and extra are optional. I use defaults if they're not specified.

My first idea was to make a django URL pattern with the required info, id and size:

url(r'^/app/(P?<id>)\d+/(P?<size>)\w+$',app.project.views.widget,name='swidget')

The optional parameters would then be a query string, like

/app/102030/large?auto=0&bw=1&extra=0

Is it a common practice to mix GET parameters with parameters defined in the URL conf in Django? Or should I make it like

url(r'^/app/(P?<id>)\d+/(P?<size>)\w+/(P?<auto>)\d/(P?<bw>)\d/(P?<extra>)\d[/]?,'app.project.views.widget,name='swidget')
#so it would look like:
/app/102030/large/0/1/0/

Any suggestions about best practices or issues to keep in mind with either style are appreciated!

like image 717
JAL Avatar asked Jan 22 '23 18:01

JAL


1 Answers

If you consider that "URL" stands for Uniform Resource Locator, the URL should only indicate the resource being displayed, and any 'configuration' options should be passed as parameters. So, I think your first idea is fine.

like image 194
harto Avatar answered Apr 06 '23 20:04

harto