Django writing a custom decorator

Viewed 2709

I am building a forum. When processing urls there is a board id and a board name. The name is for readability and the id is what fetches the board. This means if the name is wrong or has changed I want to redirect the user to the proper url. Some searching has led me to decorators but I cannot find any resource teaching me how to use them.

# urls.py
...
url(r'^boards/(?P<board_id>\d+)/(?P<board_name>[^/]+)/$', views.board, name='board'),
...

# views.py
@redirect_if_wrong_boardname
def board(request, board_id, board_name):
    ...
    return render(request, 'forums/board.html', {'board': board})

How would I implement the following logic in a decorator?

board = Board.objects.all().get(pk=pk)
    if (board.name != name):
        return redirect(request.get_full_path().replace(name, board.name, 1))
1 Answers
Related