Skip to content

Instantly share code, notes, and snippets.

@vitorfs
Created September 30, 2017 15:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitorfs/4d3334a0daa9e7a872653a22ff39320a to your computer and use it in GitHub Desktop.
Save vitorfs/4d3334a0daa9e7a872653a22ff39320a to your computer and use it in GitHub Desktop.
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.shortcuts import render, redirect, get_object_or_404
from .forms import NewTopicForm
from .models import Board, Topic, 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)
user = User.objects.first() # TODO: get the currently logged in user
if request.method == 'POST':
form = NewTopicForm(request.POST)
if form.is_valid():
topic = form.save(commit=False)
topic.board = board
topic.starter = user
topic.save()
post = Post.objects.create(
message=form.cleaned_data.get('message'),
topic=topic,
created_by=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})
@yunus9175
Copy link

so whats a right naming of boards/views.py

@zecaclasher
Copy link

zecaclasher commented Oct 11, 2019

so whats a right naming of boards/views.py

There are no models here, only views.

@EmmanuelObi
Copy link

can anyone help.
i'm getting this error while running test:
Traceback (most recent call last):
File "C:\Users\PAVILION\Desktop\Development\Django projects\myproject\boards\tests\test_view_new_topic.py", line 19, in test_new_topic_view_success_status_code
self.assertEquals(response.status_code, 200)
AssertionError: 302 != 200

@Tsh-kazi
Copy link

thanks man its working correctly

@Jeph2001
Copy link

this codes isn't working

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