def add_article(request): # username = request.session.get('user') username = request.user.username user = User.objects.get(username=username) print user addarticle=AddArticleForm(request.POST) if addarticle.is_valid() : #死机在此处 tag = Tag.objects.create(name = addarticle.cleaned_data["tag"],user=user) category= Category.objects.create(name = addarticle.cleaned_data["category"]) #tag.save() article = Article.objects.create(user=user, title=addarticle.cleaned_data["title"], desc=addarticle.cleaned_data["desc"], content=addarticle.cleaned_data["content"], category=category, date_publish = datetime.datetime.now() ) article.save() article.tag.add(tag) else : return render(request, 'blog/failure.html', {'reason': addarticle.errors}) #return render(request, 'blog/public_article.html', {'article': article, 'user': user, 'List': json.dumps(['文章发布成功', ])}) return redirect(reverse(blog.views.index))
def post(self, request): form_article = AddArticleForm(request.POST) if form_article.is_valid(): form_article.save() return redirect('/add_article/') else: context = { 'form_article': form_article, } return render(request, 'add_article.html', context)
def add_article(request): if request.method == 'POST': form = AddArticleForm(data=request.POST) if form.is_valid(): article = form.save(commit=False) article.user = request.user article.save() return redirect('home') else: form = AddArticleForm() return render(request, 'blog/edit-article.html', {'form': form})
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})
def get(self, request): form_article = AddArticleForm() context = { 'form_article': form_article, } return render(request, 'add_article.html', context)