Skip to content

Patterns for Web Development

gesellc edited this page Aug 28, 2016 · 6 revisions

Request handling (GET and POST)

  • Always redirect after a POST

  • Django Pattern: Processing POST Requests in the Same View as Renders the Form

    This time we’ll use a slightly different approach, one that’s actually a very common pattern in Django, which is to use the same view to process POST requests as to render the form that they come from. Whilst this doesn’t fit the REST-ful URL model quite as well, it has the important advantage that the same URL can display a form, and display any errors encountered in processing the user’s input.

Coding Conventions

Site Operation

Django

Complex Views

In Django, a complex view is a code smell. Could some of that logic be pushed out to a form? Or to some custom methods on the model class? Or maybe even to a non-Django module that represents your business logic?

Thin views *

If you find yourself looking at complex views, and having to write a lot of tests for them, it’s time to start thinking about whether that logic could be moved elsewhere: possibly to a form, like we’ve done here. Another possible place would be a custom method on the model class. And—​once the complexity of the app demands it—​out of Django-specific files and into your own classes and functions, that capture your core business logic.

Clone this wiki locally