def addRecipe(): if 'email' not in session: return redirect(url_for('login')) recForm = AddArticleForm() # Temporarily passing this message object in order to display an added message under the form. message = "" if request.method == 'POST': if not recForm.validate(): return render_template('newrecipe.html', recForm=recForm, message=message) else: # The Recipe constructor adds each ingredient in this tuple as a child which handles the many-to-many relationship ingredients = recForm.recipeingredients.data newRec = Recipe(recForm.recipetitle.data, recForm.recipedesc.data, ingredients, session['email']) # No Try/Catch here because the db is not configured to require unique titles db.session.add(newRec) db.session.commit() message = "Recipe added: " + newRec.recipetitle return redirect(url_for('index')) elif request.method == 'GET': return render_template('newrecipe.html', recForm=recForm, message=message)
def done(self, request, cleaned_data): try: logger.info(u'Adding a new article "{0}" to database...'.format(cleaned_data['title'])) reviewed_form = AddArticleForm(cleaned_data) logger.debug(u'title: {0}'.format(cleaned_data['title'])) logger.debug(u'body: {0}'.format(cleaned_data['body'][:50])) logger.debug(u'first_name: {0}'.format(cleaned_data['first_name'])) logger.debug(u'last_name: {0}'.format(cleaned_data['last_name'])) logger.debug(u'organization_title: {0}'.format(cleaned_data['organization_title'])) logger.debug(u'phone: {0}'.format(cleaned_data['phone_number'])) logger.debug(u'email: {0}'.format(cleaned_data['email_address'])) logger.debug(u'signature: {0}'.format(cleaned_data['signature'])) reviewed_form.instance.user = request.user logger.debug(u'User ID: {0}'.format(reviewed_form.instance.user.id)) # already provided as default values in model definition #reviewed_form.instance.create_date = reviewed_form.instance.modify_date = timezone.now() #logger.debug(u'Date created: {0}'.format(reviewed_form.instance.create_date)) #logger.debug(u'Date modified: {0}'.format(reviewed_form.instance.modify_date)) reviewed_form.save() logger.info(u'Article has been added successfully.') return render_to_response('articles/success.html', { 'message' : 'Ačiū! Jūsų straipsnis buvo sėkmingai pateiktas. Svetainės administratoriai artimiausiu metu perskaitys Jūsų straipsnį ir nedelsiant paskelbs jį svetainėje. Jei kiltų neaiškumų, susisieksime su Jumis tiesiogiai.', }, context_instance=RequestContext(request)) except: logger.exception('Exception thrown while adding a new article:')
def add_article(): form = AddArticleForm() if request.method == 'POST': sql = "insert into articles (user_id, title, article) values (%s, %s, %s)" try: title = form.title.data text = form.content.data g.cur.execute(sql, [session['user_id'], title, text]) g.db.commit() except Exception as e: mylogger.error(str(e)) g.db.rollback() abort(401) #print(text) # do something interesting with the Markdown text # allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code', \ #'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul','h1', 'h2', 'h3', 'p','img'] # html_body=markdown(text,output_format='html') # html_body=bleach.clean(html_body,tags=allowed_tags,strip=True) # html_body=bleach.linkify(html_body) # # pagedown = html_body # print(pagedown) return render_template('addarticle.html', form=form)
def front_add_article(request): # 添加文章 # 1.请求页面时,我们需要获取所有的标签和分类 # 2.提交修改后的页面时,检查分类和标签在数据库中是否存在, # 如果不存在则新建,否则存入 # 3.文章与标签是多对多关系,与其他内容不一起存 # 4.文章与分类不是是多对多关系 if request.method == 'GET': tags = TagModel.objects.all() categorys = CategoryModel.objects.all() context = {'tags': tags, 'categorys': categorys} return render(request, 'front_add_article.html', context=context) else: form = AddArticleForm(request.POST) if form.is_valid(): user = request.user tag_ids = request.POST.getlist('tags[]') title = form.cleaned_data.get('title') thumbnail = form.cleaned_data.get('thumbnail') content = form.cleaned_data.get('content') category_id = form.cleaned_data.get('category') category = CategoryModel.objects.filter(pk=category_id).first() article_model = ArticleModel(username=user, title=title, thumbnail=thumbnail, content=content, category=category) article_model.save() # 文章与标签的多对多的保存 # 如果选择了标签,则将选择的标签以此存入,否则不用存 if tag_ids: for tag_id in tag_ids: tag_model = TagModel.objects.filter(pk=tag_id).first() if tag_model: article_model.tags.add(tag_model) return myjson.json_result() else: return render(request, 'front_add_article.html', {'error': form.get_error()})
def add_article(request): if request.method == 'GET': categorys = CategoryModel.objects.all() tags = TagModel.objects.all() data = {'categorys': categorys, 'tags': tags} return render(request, 'cms_add_article.html', data) else: form = AddArticleForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') category = form.cleaned_data.get('category') desc = form.cleaned_data.get('desc') thumbnail = form.cleaned_data.get('thumbnail') content_html = form.cleaned_data.get('content_html') tags = request.POST.getlist('tags[]') user = request.user categoryModel = CategoryModel.objects.filter(pk=category).first() articleModel = ArticleModel(title=title, desc=desc, thumbnail=thumbnail, content_html=content_html, author=user, category=categoryModel) articleModel.save() #添加tags的多对多 if tags: for tag in tags: tagModel = TagModel.objects.filter(pk=int(tag)).first() if tagModel: articleModel.tags.add(tagModel) return xtjson.json_result(data={}) else: return form.get_error_response()
def front_add_article(request): if request.method == 'GET': categorys = Categorys.objects.all() tags = Tags.objects.all() context = {'categorys': categorys, 'tags': tags} if hasattr(request, 'front_user'): context['front_user'] = request.front_user return render(request, 'front_add_article.html', context) else: form = AddArticleForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') subtitle = form.cleaned_data.get('subtitle') data = request.POST tags = request.POST.getlist('tags[]') context = {} for k, v in data.iteritems(): context[k] = v category_name = context['category'] #获取分类 categoryModel = Categorys.objects.filter( name=category_name).first() current_user = request.front_user article_content = context['textarea_content'] articleModel = Articles(title=title, subtitle=subtitle, categorys=categoryModel, author=current_user, content=article_content) articleModel.save() for tag in tags: tagModel = Tags.objects.filter(pk=tag).first() articleModel.tags.add(tagModel) data = {'code': 200, 'info': u'恭喜您!文章发布成功!'} return JsonResponse(data) else: return render(request, 'front_add_article.html', {'error': '表单验证出错!'})
def AddArticle(request): if request.method == 'POST': article_tag = request.POST.get('tag', '') article_title = request.POST.get('title', '') article_content = request.POST.get('content', '') add_article_form = AddArticleForm(request.POST) print article_tag print article_title print article_content print add_article_form.errors if add_article_form.is_valid(): article = add_article_form.save(commit=False) article.author = request.user article.save() return redirect('/blog/index') else: return HttpResponseRedirect('/') else: return redirect('/user/add/')