Skip to content

Instantly share code, notes, and snippets.

@vitorfs

vitorfs/views.py Secret

Created September 30, 2017 17:23
Show Gist options
  • Save vitorfs/3d73ef25a01eceea07ef3ad8538437cf to your computer and use it in GitHub Desktop.
Save vitorfs/3d73ef25a01eceea07ef3ad8538437cf 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, 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})
@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})
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})
@shreyansh-agarwal
Copy link

Im Getting this error. Tried a lot but couldnt get through:

NoReverseMatch at /boards/1/topics/1/
Reverse for 'boards_topics' with arguments '('',)' not found. 1 pattern(s) tried: ['boards/(?P\d+)/$']

@Jaimish00
Copy link

hmm ..why is it board__pk ?

yeah.......it should be board_id

@joyc
Copy link

joyc commented Oct 10, 2020

hmm ..why is it board__pk ?

yeah.......it should be board_id

yes, board_id works

@Chinwendu20
Copy link

Im Getting this error. Tried a lot but couldnt get through:

NoReverseMatch at /boards/1/topics/1/
Reverse for 'boards_topics' with arguments '('',)' not found. 1 pattern(s) tried: ['boards/(?P\d+)/$']

Did you later figure this out?

@nwokolo24
Copy link

Im Getting this error. Tried a lot but couldnt get through:
NoReverseMatch at /boards/1/topics/1/
Reverse for 'boards_topics' with arguments '('',)' not found. 1 pattern(s) tried: ['boards/(?P\d+)/$']

Did you later figure this out?

Hi Chinwendu, did you find the solution for this?

@nwokolo24
Copy link

Im Getting this error. Tried a lot but couldnt get through:
NoReverseMatch at /boards/1/topics/1/
Reverse for 'boards_topics' with arguments '('',)' not found. 1 pattern(s) tried: ['boards/(?P\d+)/$']

Did you later figure this out?

The numbering of topics does not start newly for every board, but is global for all topics, regardless of the board, they are created in. Therefore topics/1/ refers to the first topic you created on the whole forum. If this was in a different board, then boards/1/topics/1/ does not exist. There might exist boards/2/topics/1/ or boards/3/topics/1/ or ... depending on the board, where you created the very first topic. Also you might find boards/1/topics/2/ or boards/1/topics/3/ or ... depending on the order in which you created topics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment