Guest User

views.py

a guest
Jul 12th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. from django.shortcuts import render, redirect
  2. from collection.forms import QuestionForm
  3. from collection.models import Questions
  4.  
  5. def index(request):
  6. # urls.py will catch that someone wants the homepage and points to this
  7. # piece of code which will render the index.html template
  8. # render combines a given template with a given context dictionary and
  9. # returns a HTTPResponse object with that rendered text
  10. # render(request, template_name)
  11. # When the index page is viewed, find all the questions in our DB, display
  12. # this template, and pass those things along to the template
  13. #questions = Questions.objects.filter(name__contains='Unique String')
  14. questions = Questions.objects.all()
  15. return render(request, 'index.html', {
  16. 'questions': questions,
  17. })
  18.  
  19.  
  20. def question_detail(request, slug):
  21. # grab the object
  22. question = Questions.objects.get(slug=slug)
  23. # and pass to the template
  24. return render(request, 'questions/questions_detail.html', {
  25. })
  26.  
  27. def edit_question(request, slug):
  28. # grab the object
  29. question = Questions.objects.get(slug=slug)
  30. # set the form we're using...
  31. form_class = QuestionForm
  32. # if we're coming tot his view from a submitted form,
  33. if request.method == 'POST':
  34. # grab the data from the submitted form
  35. form = form_class(data=request.POST, instance=question)
  36. if form.is_valid():
  37. # save the new data
  38. form.save()
  39. return redirect('questions_detail', slug=question.slug)
  40.  
  41. # otherwise just create the form
  42. else:
  43. form = form_class(instance=question)
  44.  
  45. # and render the template
  46. return render(request, 'questions/edit_question.html', {
  47. 'question': question,
  48. 'form': form,
  49. })
Add Comment
Please, Sign In to add comment