def add_questions(modeladmin, request, queryset): '''批量添加问题''' opts = modeladmin.model._meta app_label = opts.app_label if request.POST.get('post'): form = AddQuestionForm([], initial={ '_selected_action': request.POST.getlist( admin.ACTION_CHECKBOX_NAME) }, data=request.POST) n = queryset.count() if n: if form.is_valid(): question = Question.objects.get( id=form.cleaned_data['question_id']) for article in queryset: article.questions.add(question) modeladmin.message_user(request, '成功修改%d个产品' % n) return None else: modeladmin.message_user(request, '输入有误,请重新提交;%s' % str(form.errors)) pass else: modeladmin.messag_user(request, '未选择任何数据') return None title = '添加问题' action = 'add_questions' form = AddQuestionForm( [], initial={'_selected_action': [i.id for i in queryset]}) context = { 'title': title, 'queryset': queryset, 'count': queryset.count(), 'opts': opts, 'app_label': app_label, 'action': action, 'action_checkbox_name': admin.ACTION_CHECKBOX_NAME, 'form': form, } return TemplateResponse(request, 'zhufu/admin/add_questions.html', context=context, current_app=modeladmin.admin_site.name)
def add_question(request): context = {} num = IntegerSetting.get('question_number_of_answers').get_value() if request.method == 'POST': form = AddQuestionForm(request.POST) if form.is_valid(): new_question = form.save() return redirect('qpool_home', cat=new_question.category.name) else: form = AddQuestionForm() answers_range = [str(i) for i in xrange(1, num + 1)] categories = [(c.name.capitalize(), c.name) for c in Category.objects.all()] context['categories'] = categories context['answers_range'] = answers_range context['form'] = form return render_to_response('cpanel/add_question.html', context, context_instance=RequestContext(request))
def add_questions(modeladmin, request, queryset): '''批量添加问题''' opts = modeladmin.model._meta app_label = opts.app_label if request.POST.get('post'): form = AddQuestionForm([], initial={'_selected_action': request.POST.getlist(admin.ACTION_CHECKBOX_NAME)}, data = request.POST) n = queryset.count() if n: if form.is_valid(): question = Question.objects.get(id=form.cleaned_data['question_id']) for article in queryset: article.questions.add(question) modeladmin.message_user(request, '成功修改%d个产品'%n) return None else: modeladmin.message_user(request, '输入有误,请重新提交;%s'%str(form.errors)) pass else: modeladmin.messag_user(request, '未选择任何数据') return None title = '添加问题' action = 'add_questions' form = AddQuestionForm([], initial={'_selected_action': [i.id for i in queryset]}) context = { 'title': title, 'queryset': queryset, 'count': queryset.count(), 'opts': opts, 'app_label': app_label, 'action': action, 'action_checkbox_name': admin.ACTION_CHECKBOX_NAME, 'form': form, } return TemplateResponse(request, 'zhufu/admin/add_questions.html', context=context, current_app=modeladmin.admin_site.name)
def ask_question(request): form = AddQuestionForm(request.POST or None) if request.method == "POST" and form.is_valid(): new_question = Question(title=form.cleaned_data['title'], content=form.cleaned_data['content'], author=request.user) new_question.save() tags_list = form.cleaned_data['tags'].split(" ") tags = [] for tag_title in tags_list: t = Tag.objects.get_or_create(title=tag_title) tags.append(t) for (key, value) in tags: new_question.tags.add(key) new_question.save() return redirect(new_question) return render(request, 'addquestion.html', { 'form': form, })