def question_create(request): if request.method == 'POST': question_form = QuestionForm(request.POST) if question_form.is_valid(): question_post = question_form.save(commit=False) question_post.author = request.user question_post.save() tag_ids = request.POST.get('tags').split(',') print(tag_ids) for tag_id in tag_ids: if tag_id == None: tag_id = 8 question_tag = QuestionTag(tag_id=tag_id, question_id=question_post.id) question_tag.save() messages.success( request, f'Your question has been added!', extra_tags='success') return redirect('questions-home') else: messages.error(request, f'Something went wrong!', extra_tags='danger') else: question_form = QuestionForm() context = { 'question_form': question_form, } return render(request, 'questions/question_create.html', context)
def add_question(text, imgpath, tagwords=None): ''' text: question text imgpath: path to the image ''' if tagwords is None: tagwords = [] question = Question(text=text, diagram=imgpath) question.save() for tagword in tagwords: if QuestionTag.objects.filter(word=tagword).exists(): tag = QuestionTag.objects.get(word=tagword) else: tag = QuestionTag(word=tagword) tag.save() question.tags.add(tag) # Update new tags question.save()
def add_question(text, imgpath, tagwords=None): ''' text: question text imgpath: path to the image ''' if tagwords is None: tagwords = [] question = Question(text=text,diagram=imgpath) question.save() for tagword in tagwords: if QuestionTag.objects.filter(word=tagword).exists(): tag = QuestionTag.objects.get(word=tagword) else: tag = QuestionTag(word=tagword) tag.save() question.tags.add(tag) # Update new tags question.save()