def question(request, questionId):#page with answers profile = Profile.objects.get_profile(request.user) question = get_object_or_404(Question, pk=questionId) tbags = Tag.objects.all() answers = Answer.objects.get_answers_to_question(questionId) page, paginator = paginate(answers, request) form = AnswerForm() error_message = '' if request.method == 'POST': form = AnswerForm(request.POST) if form.is_valid(): Answer.objects.create_answer(profile, form.cleaned_data['answerTextField'], question) return redirect('/ask/question/' + str(questionId)) else: error_message = form.errors # 'Form\'s not valid' context = { 'question': question, 'answers': page, 'paginator': paginator, 'tags': tbags, 'form': form, 'error_message': error_message, 'profile': profile, 'user': request.user, } context.update(csrf(request)) return render_to_response('question.html', context)
def question(request): try: question_id = int(request.GET.get('id')) except ValueError: raise Http404("id не число") current_question = Question.objects.get(pk=question_id) if request.method == 'POST': if not request.user.is_authenticated: # todo вернуть ошибку pass form = AnswerForm(request.POST) if form.is_valid(): ans = Answer.objects.create(author=request.user, question=current_question, text=form.cleaned_data['text']) return HttpResponseRedirect(request.get_full_path()) else: # todo вернуть ошибку pass else: form = AnswerForm() list_answers = current_question.answer_set.all() context = { 'list_answers': list_answers, 'question': current_question, 'form': form, } return render(request, 'question.html', context)
def question(request, **kwargs): qid = kwargs['qid'] if qid is None: raise Http404() _question = Question.objects.get(pk=qid) _answers = Answer.objects.filter(question_id=qid) top_tags = get_popular_tags() best_members = get_best_members() answers, paginator = paginate(_answers, request) if request.method == 'POST': if not request.user.is_authenticated(): response = redirect('login') response['Location'] += '?next=' + qid return response form = AnswerForm(request.POST) if form.is_valid(): answer = form.save(commit=False) answer.question_id = qid answer.author = Profile.objects.get(user=request.user) answer.save() anchor = '#' + str(answer.pk) url = resolve_url('question', _question.pk) return redirect(url + anchor) else: form = AnswerForm() else: form = AnswerForm() context = { 'question': _question, 'top_tags': top_tags, 'best_members': best_members, 'paginator': paginator, 'answers': answers, 'form': form } return render(request, 'question.html', context)
def post(self, request, id): try: profile = Profile.objects.get(user=request.user) except Profile.DoesNotExist: profile = Profile(user=request.user) q = Question.objects.get(pk=id) answers = Answer.objects.filter(question=q) answers = paginate(answers, request) form = AnswerForm(data=request.POST, user=profile, id=id) if form.is_valid(): form.save() return HttpResponseRedirect("../question/" + str(id)) return render(request, 'questions.html', { "question": q, "answers": answers, 'form': form })
def answer(request, question_id): answer = Answer.answers.answer(question_id) during_question = Question.questions.question(question_id) if request.POST: form = AnswerForm(request.POST) if form.is_valid(): text = form.cleaned_data.get('text') author = request.user question = during_question a = Answer( text = text, author = author, question = question) a.save() return redirect('/') form = AnswerForm() return render(request, 'answer.html', { 'answers' : answer, 'question' : during_question, 'form' : form })
def question(request, qid, page=1): try: q = Question.objects.get_single(int(qid)) answersObjs = Answer.objects.by_id(q.id) answers, page_range = paginate(answersObjs, 4, page) if request.method == "POST": form = AnswerForm(request.POST) if form.is_valid(): answer, page = form.save(request, q) print(connection.queries) return HttpResponseRedirect('/question/' + str(q.id) + '/' + str(page)) else: form = AnswerForm() except Question.DoesNotExist: raise Http404() context = { "question": q, "answers": answers, "page_range": page_range, "pag_url": 'questions', "form": form, } print(connection.queries) return render(request, 'question.html', context)
def newanswer(request, question_id): if request.user.is_authenticated(): q = get_object_or_404(Question, pk=question_id) u = request.user user = u.customuser answer_form = AnswerForm(request.POST) # A form bound to the POST data if answer_form.is_valid(): # All validation rules pass answer = Answer( ansver_text=answer_form.cleaned_data["ansver_text"], customuser=user, question=q, pub_data=timezone.now(), flag=0, ) answer.save() request.session["message"] = "Your question was successfully added!!" request.session["flag"] = 1 else: request.session["message"] = "Error!!" request.session["flag"] = 1 else: request.session["message"] = "You must be logged in" request.session["flag"] = 0 return HttpResponseRedirect(reverse("detail", args=(question_id,))) # Redirect after POST
def question(request, question_id, page=None): ques = Question.objects.get(id=question_id) answers = ques.answer_set.all() answers_on_page = paginate(answers, request, 10, page) User = getAuthenticatedUser(request) if User != None: if request.POST: form = AnswerForm(request.POST) if form.is_valid(): answer = form.saveAnswer(User, question_id) page = answers.count() / 10 if (answers.count() % 10 != 0): page += 1 return redirect('/question/' + str(question_id) + '/' + str(page)) else: form = AnswerForm() else: form = AnswerForm() return render(request, 'ask/question.html', { 'answers': answers_on_page, 'question': ques, 'page': page, 'form' : form, })
def question(request, question_number): question = get_object_or_404(Question, id=question_number) since = request.GET.get('since') answers, since = Answer.objects.main(since, question) if request.method == "POST": form = AnswerForm(request.POST) if form.is_valid(): if request.user.is_anonymous: answer = form.save(question_id=question_number) else: answer = form.save(user=request.user, question_id=question_number) question = answer.question return HttpResponseRedirect("/question/{}/".format(question.id)) else: form = AnswerForm() return render(request, 'ask/question.html', { 'answers': answers, 'question': question, 'since': since, 'form': form, 'username': request.user.username })