Skip to content

Instantly share code, notes, and snippets.

@vitorfs vitorfs/ Secret
Created Sep 30, 2017

What would you like to do?
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, Topic
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})
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 =
topic.board = board
topic.starter = request.user
return redirect('board_topics', # TODO: redirect to the created topic page
form = NewTopicForm()
return render(request, 'new_topic.html', {'board': board, 'form': form})
def topic_posts(request, pk, topic_pk):
topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk)
return render(request, 'topic_posts.html', {'topic': topic})

This comment has been minimized.

Copy link

skimpa commented Jun 19, 2018

hmm ..why is it board__pk ?


This comment has been minimized.

Copy link

nayamama commented Jun 28, 2018

double underscore are used for field lookup. It is similar to SQL WHERE clause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.