Skip to content

Instantly share code, notes, and snippets.

@vitorfs

vitorfs/views.py Secret

Created September 30, 2017 16:41
Show Gist options
  • Save vitorfs/483936caca4618dc275545ad2dfbef24 to your computer and use it in GitHub Desktop.
Save vitorfs/483936caca4618dc275545ad2dfbef24 to your computer and use it in GitHub Desktop.
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, redirect, render
from .forms import NewTopicForm
from .models import Board, Post
def home(request):
boards = Board.objects.all()
return render(request, 'home.html', {'boards': boards})
def board_topics(request, pk):
board = get_object_or_404(Board, pk=pk)
return render(request, 'topics.html', {'board': board})
@login_required
def new_topic(request, pk):
board = get_object_or_404(Board, pk=pk)
if request.method == 'POST':
form = NewTopicForm(request.POST)
if form.is_valid():
topic = form.save(commit=False)
topic.board = board
topic.starter = request.user
topic.save()
Post.objects.create(
message=form.cleaned_data.get('message'),
topic=topic,
created_by=request.user
)
return redirect('board_topics', pk=board.pk) # TODO: redirect to the created topic page
else:
form = NewTopicForm()
return render(request, 'new_topic.html', {'board': board, 'form': form})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment