Skip to content

Commit

Permalink
Add a few tips on URL naming #15
Browse files Browse the repository at this point in the history
  • Loading branch information
IwoHerka committed Oct 14, 2018
1 parent 0e1b3b8 commit 5abc08a
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion django.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cookiecutter [email protected]:makimo/django-template.git
### Templates

Nobody cares whether HTML in the browser is indented nicely or not. So the
focus shifts to make templates programmer-friendly, treating logical blocks
focus shifts to make templates programmer-friendly, treating logical blocks
and HTML elements as first-class citizens for legibility.

```
Expand All @@ -31,3 +31,50 @@ and HTML elements as first-class citizens for legibility.
```

Note: You can omit indentation after global scope `{% block %}` elements.

### URL Naming

Here are some tips for creating readable URL resource structure.

#### 1. Path Params vs. Query Strings

Use path params to locate the resource and query strings to parametrize
the query.

```python
def get(self, request, object_id):
option = self.request.GET.get('option', 'default')
```

#### 2. Plural vs. Singular

Use plural form to indicate the fact that a resource is a list. Use
singular if object returned is a singleton.

```
/users
/users/{id}/config
```

#### 3. Nesting Resources

A good rule of thumb regarding resource resting is to nest resources if
a child candidate resources cannot exist outside of its parent. Otherwise
we may end up with multiple paths refering to the same resource.

```
/users/{user_id}/config
/user-configs?user_id={user_id}
/user-configs/{user_id}
/configs?type=user-configs&cfg_id={cfg_id}
```

This is very ugly.

Of course, it _may_ be more convienient and readable to break this rule.
In such case, ensure that nesting is done only on strong relations.

```
/author/{author_id}/books
/books/{book_id}
```

0 comments on commit 5abc08a

Please sign in to comment.