def addtag(request): tag_name=request.GET.get('tag_name') newtag=Tag() newtag.tag_name=tag_name newtag.save() id=newtag.id tag={'id':id,'tag_name':tag_name} tag=json.dumps(tag) return HttpResponse(tag)
def save_new_review_article(request): if request.method == 'POST' and request.user.is_authenticated(): form = AddArticleForm(request.POST) work_id = request.POST.get('work_id') work = Work.objects.get(pk=work_id) artist = work.creator if form.is_valid(): article = form.save() author = Author.objects.filter(user=request.user)[0] article.author = author article.work_link = work article.artist_link = artist article.save() tags_string = request.POST.get('tags') tags_list = tags_string.split(',') tags = [] for tag in tags_list: tags.append(tag.strip().lower().replace('-', ' ').replace('_', ' ')) # A Tag is another model, linked to the "article" via a ManyToMany field # Here we check to see if the user has entered tags that already exist in the database for tag in tags: pre_existing_tags = Tag.objects.filter(tag_name=tag) if len(pre_existing_tags) > 0: pre_existing_tag = pre_existing_tags[0] article.tags.add(pre_existing_tag) article.save() else: #create a new tag, save it to the DB, and add it to the article new_tag = Tag() new_tag.tag_name = tag new_tag.save() article.tags.add(new_tag) article.save() return HttpResponseRedirect('/wc_admin/view_article/?id=' + str(article.id)) else: error_message = 'The form was not valid. The data was not saved.' return render(request, 'blog/error.html', {'error_message': error_message, 'form': form}) else: error_message = 'The info was not properly posted. The data was not saved.' return render(request, 'blog/error.html', {'error_message': error_message})