Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-yin committed Nov 1, 2023
1 parent 70a3f15 commit 48c4f59
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 17 deletions.
20 changes: 5 additions & 15 deletions docs/source/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,26 +320,16 @@ to ensure the FormData object includes the button value.

## Redirects

As per the [documentation](https://turbo.hotwire.dev/handbook/drive#redirecting-after-a-form-submission), Turbo expects a 303 redirect after a form submission. While this does not appear to be a hard-and-fast rule, you should probably have your view return a 303 instead of a 301 or 302 after a form submission. This package includes a class **turbo_response.HttpResponseSeeOther** and a shortcut **redirect_303** for returning the correct status with a redirect. The form mixin and view classes will return a 303 redirect by default.
As per the [documentation](https://turbo.hotwire.dev/handbook/drive#redirecting-after-a-form-submission), Turbo expects a 303 redirect after a form submission.

```python
from turbo_response import HttpResponseSeeOther

def my_view(request):
form = MyForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseSeeOther("/")
```
If your project has `PUT`, `PATCH`, `DELETE` requests, then you might need to take a look at this [Clarification on redirect status code (303)](https://github.com/hotwired/turbo/issues/84#issuecomment-862656931)

Note that the **redirect_303** shortcut works the same way as **django.shortcuts.redirect**: you can use a view name with arguments, a URL string, or a model which has a `get_absolute_url()` method:
In Django, you can do it like this:

```python
from turbo_response import redirect_303
from django.shortcuts import redirect

redirect_303("/")
redirect_303("blog_detail", id=1, slug=blog.title)
redirect_303(blog)
return redirect('https://example.com/', status=303)
```

## Responding with Multiple Streams
Expand Down
2 changes: 1 addition & 1 deletion src/turbo_response/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class HttpResponseSeeOther(HttpResponseRedirect):
"""Redirect with 303 status"""
"""Obsolete: Redirect with 303 status"""

status_code = http.HTTPStatus.SEE_OTHER

Expand Down
2 changes: 1 addition & 1 deletion src/turbo_response/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def redirect_303(to: Union[str, Model], *args, **kwargs) -> HttpResponseSeeOther:
"""Sends an HTTP 303 redirect.
"""Obsolete: Sends an HTTP 303 redirect.
:param to: URL or view name or model instance. If model then calls `get_absolute_url()`.
"""
Expand Down

0 comments on commit 48c4f59

Please sign in to comment.