def edit(request, id): tags = '' if id: question = get_object_or_404(Question, pk=id) for tag in question.get_tags(): tags = u'{0} {1}'.format(tags, tag.tag) tags = tags.strip() else: question = Question(create_user=request.user) if request.POST: form = QuestionForm(request.POST, instance=question) if form.is_valid(): form.save() return redirect('/questions/') else: form = QuestionForm(instance=question, initial={'tags': tags}) return render(request, 'questions/edit.html', {'form': form})
def write(request): if request.method == 'POST': form = QuestionForm(request.POST) if form.is_valid(): question = Question() question.create_user = request.user question.title = form.cleaned_data.get('title') question.content = form.cleaned_data.get('content') status = form.cleaned_data.get('status') if status in [Question.PUBLISHED, Question.DRAFT]: question.status = form.cleaned_data.get('status') question.save() tags = form.cleaned_data.get('tags') question.create_tags(tags) return redirect('/questions/') else: form = QuestionForm() return render(request, 'questions/write.html', {'form': form})