本文整理匯總了Python中questions.forms.AnswerForm類的典型用法代碼示例。如果您正苦於以下問題:Python AnswerForm類的具體用法?Python AnswerForm怎麽用?Python AnswerForm使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了AnswerForm類的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: question_details
def question_details(request, question_key):
ctx = {}
question = get_object_or_404(Question, key=question_key)
if request.method == 'POST':
user = users.get_current_user()
if user is None:
return redirect(users.create_login_url(request.path))
initial = {
'author': users.get_current_user(),
'question': question,
}
form = AnswerForm(request.POST, initial=initial)
if form.is_valid():
# XXX FIXME - why initial data does not exist in cleaned data?
form.cleaned_data.update(initial)
form.save()
return redirect(reverse('questions_question_list'))
ctx['answer_form'] = form
else:
ctx['answer_form'] = AnswerForm()
ctx['question'] = question
return render(request, 'questions/question_details.html', ctx)
示例2: edit_answer
def edit_answer(request, question_id, answer_id):
"""Edit an answer."""
answer = get_object_or_404(Answer, pk=answer_id, question=question_id)
answer_preview = None
if answer.question.is_locked:
raise PermissionDenied
# NOJS: upload images, if any
upload_imageattachment(request, answer)
if request.method == 'GET':
form = AnswerForm({'content': answer.content})
return jingo.render(request, 'questions/edit_answer.html',
{'form': form, 'answer': answer})
form = AnswerForm(request.POST)
if form.is_valid():
answer.content = form.cleaned_data['content']
answer.updated_by = request.user
if 'preview' in request.POST:
answer.updated = datetime.now()
answer_preview = answer
else:
log.warning('User %s is editing answer with id=%s' %
(request.user, answer.id))
answer.save()
return HttpResponseRedirect(answer.get_absolute_url())
return jingo.render(request, 'questions/edit_answer.html',
{'form': form, 'answer': answer,
'answer_preview': answer_preview})
示例3: add_answer
def add_answer(request, pk):
context = RequestContext(request)
registered = False
if request.method == 'POST':
answer_form = AnswerForm(data=request.POST, initial={'user': request.user, 'question': pk})
if answer_form.is_valid():
answer_form.instance.user = request.user
answer_form.instance.question = Question.objects.get(id=pk)
answer_form.save()
registered = True
else:
print answer_form.errors
else:
answer_form = AnswerForm()
return render(request, 'answer/new.html', {
'answer_form': answer_form, 'registered': registered, 'question': Question.objects.get(id=pk) })
示例4: QuestionsPage
def QuestionsPage(request):
if request.user.registered:
if request.user.user_profile.win == True:
return redirect(reverse('win_page'))
else:
if request.method == 'GET':
try:
question = Question.objects.get(current=True)
except ObjectDoesNotExist:
request.user.user_profile.win = True
request.user.user_profile.save()
return redirect(reverse('win_page'))
answer_form = AnswerForm()
return render(request,'Questions/questionspage.html',{"question":question,"answer_form":answer_form})
elif request.method == 'POST':
question = Question.objects.get(current=True)
answer_form=AnswerForm(request.POST,request.FILES)
if answer_form.is_valid():
recieved_file = request.FILES['file']
sample_file = open(os.path.join(settings.BASE_DIR,'files/output/'+question.title+".txt"), 'rb')
flag = True
line1 = recieved_file.readline()
line2 = sample_file.readline()
while line1 != bytes('#','utf-8') or line2 != bytes('#','utf-8'):
print(line1,line2)
if line1 != line2:
flag = False
break
line1 = recieved_file.readline()
line2 = sample_file.readline()
print(flag)
sample_file.close
if flag:
request.user.user_profile.win = True
request.user.user_profile.time_counter = int(format(datetime.now(), 'U'))
request.user.user_profile.save()
return redirect(reverse('win_page'))
else:
answer_form.add_error('file',"Oops, This is not the answer we expected")
return render(request,'Questions/questionspage.html',{"question":question,"answer_form":answer_form})
else:
return render(request,'Questions/questionspage.html',{"question":question,"answer_form":answer_form})
else:
return redirect(reverse('register_page'))
示例5: answer
def answer(request):
if request.method == 'POST':
form = AnswerForm(request.POST)
if form.is_valid():
user = request.user
answer = Answer()
answer.user = request.user
answer.question = form.cleaned_data.get('question')
answer.description = form.cleaned_data.get('description')
answer.save()
user.profile.notify_answered(answer.question)
return redirect(u'/questions/{0}/'.format(answer.question.pk))
else:
question = form.cleaned_data.get('question')
return render(request, 'questions/question.html', {'question': question, 'form': form})
else:
return redirect('/questions/')
示例6: reply
def reply(request, question_id):
"""Post a new answer to a question."""
question = get_object_or_404(Question, pk=question_id)
answer_preview = None
if question.is_locked:
raise PermissionDenied
form = AnswerForm(request.POST)
# NOJS: delete images
if 'delete_images' in request.POST:
for image_id in request.POST.getlist('delete_image'):
ImageAttachment.objects.get(pk=image_id).delete()
return answers(request, question_id=question_id, form=form)
# NOJS: upload image
if 'upload_image' in request.POST:
upload_imageattachment(request, question)
return answers(request, question_id=question_id, form=form)
if form.is_valid():
answer = Answer(question=question, creator=request.user,
content=form.cleaned_data['content'])
if 'preview' in request.POST:
answer_preview = answer
else:
answer.save()
ct = ContentType.objects.get_for_model(answer)
# Move over to the answer all of the images I added to the
# reply form
up_images = question.images.filter(creator=request.user)
up_images.update(content_type=ct, object_id=answer.id)
statsd.incr('questions.answer')
if Setting.get_for_user(request.user,
'questions_watch_after_reply'):
QuestionReplyEvent.notify(request.user, question)
return HttpResponseRedirect(answer.get_absolute_url())
return answers(request, question_id=question_id, form=form,
answer_preview=answer_preview)
示例7: question
def question(request, question_id):
"""
Question page
"""
data = {}
# overlay ask form
data['questionform'] = QuestionForm()
# active page title
data['active'] = 'question'
# new answer form
data['form'] = AnswerForm()
# optimized question query
q = Question.objects.filter(id=int(question_id))\
.select_related('author',
'author__user', 'author__user_username',
'author__user_id', 'answer',
'answer__author', 'answer__author__user__username',
'answer__author__user__id',
)
# check if question exists
if len(q) == 0:
raise Http404
q = q[0]
# get all answers
query = q.answers.all().select_related('author',
'author__user',
'author__user_username',
'author__user_id')
# pagination
paginator = Paginator(query, 30)
page_number = request.GET.get('page', '1')
try:
page = paginator.page(page_number)
except PageNotAnInteger:
page = paginator.page(1)
except EmptyPage:
page = paginator.page(paginator.num_pages)
data['question'] = q
data['answers'] = page
# new answer handling
if request.method == 'POST':
form = AnswerForm(request.POST)
data['form'] = form
if form.is_valid():
user = request.user
question_user = QuestionsUser.objects.filter(user=user)
# check if user exists
if len(question_user) == 0:
return HttpResponseForbidden()
# create new answer
question_user = question_user[0]
content = form.cleaned_data['content']
a = Answer(author=question_user, content=content, question=q)
a.save()
# redirect to question page
return redirect(q.get_absolute_url())
q.visit()
return render(request, "question.html", data)