def edit_post(request, username, trip_name_slug, post_name_slug): blog = BlogPost.objects.get(slug=post_name_slug) compile = {'post': blog.as_dict()} data = json.dumps(compile) form = BlogForm() if request.method == 'POST': form = BlogForm(request.POST) if form.is_valid(): blog.title = form.cleaned_data['title'] blog.content = form.cleaned_data['content'] blog.save() # Use Redirect so that URL in browser also changes return HttpResponseRedirect( reverse('view_trip', kwargs={ 'username': username, 'trip_name_slug': trip_name_slug })) else: print(form.errors) return render( request, 'app/edit_post.html', { 'json_data': data, 'form': form, 'tripslug': trip_name_slug, 'postslug': post_name_slug })
def breyta(page, no): if current_user.is_anonymous or int(no) < 0: return redirect('/index') post1 = Contact.query.get(no) form1 = ContactForm(obj=post1) if form1.validate_on_submit(): db.session.delete(post1) db.session.commit() p = Contact(title=form1.type.data, body=form1.body.data) db.session.add(p) db.session.commit() return redirect('/contact') post2 = Post.query.get(no) form2 = BlogForm(obj=post2) if form2.validate_on_submit(): db.session.delete(post2) db.session.commit() if post2.title[-8:] == '(Breytt)': p = Post(title=form2.title.data, body=form2.body.data, timestamp=post2.timestamp) else: p = Post(title=form2.title.data + ' (Breytt)', body=form2.body.data, timestamp=post2.timestamp) db.session.add(p) db.session.commit() return redirect('/updates') return render_template('breyta.html', page=page, no=no, form1=form1, form2=form2)
def write(): form = BlogForm() if form.validate_on_submit(): user = current_user._get_current_object() p = Blog(title=form.title.data, text=form.text.data, user=user) db.session.add(p) return redirect(url_for('main.index')) return render_template('posts/write.html', form=form)
def updates(): form = BlogForm() if form.validate_on_submit(): p = Post(title=form.title.data, body=form.body.data) db.session.add(p) db.session.commit() return redirect('/updates') posts = Post.query.all() return render_template('updates.html', form=form, posts=posts)
def breyta2(no): if current_user.is_anonymous or int(no) < 0: return redirect('/index') post3 = Index.query.get(no) form3 = BlogForm(obj=post3) if form3.validate_on_submit(): db.session.delete(post3) db.session.commit() p = Index(title=form3.title.data, body=form3.body.data) db.session.add(p) db.session.commit() return redirect('index') return render_template('breyta2.html', no=no, form=form3)
def dashboard(): """takes the user to dashboard""" if not session.get("user_id") is None: form = BlogForm() user = session.get("username") user_id = session.get("user_id") if form.validate_on_submit(): new_blog = Blog( blog_title=form.blog_title.data, blog_type=form.blog_type.data, blog_desc=form.blog_desc.data, blog_content=form.blog_content.data, blog_user_id=user_id, ) db.session.add(new_blog) db.session.commit() return redirect(url_for("show_my_blogs")) return render_template("dashboard.html", form=form, user=user) else: return redirect(url_for("page_error"))
def newpost(request): # лаба 10 v """Renders the newpost page.""" assert isinstance(request, HttpRequest) if request.method == 'POST': # после отправки формы blogform = BlogForm(request.POST, request.FILES) if blogform.is_valid(): blog_f = blogform.save(commit=False) blog_f.posted = datetime.now() blog_f.author = request.user blog_f.save() #сохраняем изменения после добавления полей return redirect( 'blog' ) # переадресация на страницуц Блог после создания статьи Блога else: blogform = BlogForm() # создание объекта формы ввода данных return render( request, 'app/newpost.html', { 'blogform': blogform, # передача формы в шаблон веб-страницы 'title': 'Добавить статью блога', 'year': datetime.now(), })
def shop(): form = BlogForm() # topics = Post(topic_id=0) # db.session.add(topics) #db.session.commit() posts = Post.query.all() if form.validate_on_submit(): if current_user.is_authenticated: post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() # a = Post.query.order_by(Post.topic_id.desc()).all() # for updated in a: # b = str(updated) # if len(b) > 4: # print("this means it doesn;t work kinda idk") # updated_list.append(updated) # flash('Your post is now live!') return redirect(url_for('shop')) else: return redirect(url_for('signup')) return render_template('shop.html', form=form, posts=posts)
def add_blog(): form = BlogForm() if form.validate_on_submit(): new_blog = Blog(title=form.title.data, content=mistune.html(form.content.data)) if form.tags.data: blog_tags = [x.strip() for x in form.tags.data.split(",")] for tag in blog_tags: present_tag = Tag.query.filter_by(name=tag).first() if (present_tag): present_tag.blogs_associated.append(new_blog) else: new_tag = Tag(name=tag) new_tag.blogs_associated.append(new_blog) db.session.add(new_tag) db.session.add(new_blog) db.session.commit() return redirect(url_for('index')) return render_template('add_blog.html', title='add blog', form=form)
def fan_message(): form=BlogForm() topics = Post(topic_id=0) db.session.add(topics) db.session.flush() # a = Post.query.order_by(Post.topic_id.desc()).all() # for updated in a: # b = str(updated) # if len(b) > 4: # print("this means it doesn;t work kinda idk") # updated_list.append(updated) #if form.validate_on_submit(): # post = Post(body=form.post.data) # db.session.add(post) # db.session.flush() # flash('Your post is now live!') # return redirect(url_for('iframe_popup')) return render_template('fan_message.html',form=form)
def newpost(request): if request.method == "POST": blogform = BlogForm(request.POST, request.FILES) if blogform.is_valid(): blog_f = blogform.save(commit=False) blog_f.posted = datetime.now() blog_f.save() return redirect('blog') else: blogform = BlogForm() assert isinstance(request, HttpRequest) return render(request, 'app/newpost.html', { 'blogform': blogform, 'year': datetime.now().year, })
def add_blog_post(request, username, trip_name_slug): form = BlogForm() if request.method == 'POST': form = BlogForm(request.POST) if form.is_valid(): blog = form.save(commit=False) blog.user = request.user blog.Date = datetime.today() trip = get_object_or_404(Trip, slug__exact=trip_name_slug) blog.trip = trip blog.save() # Use Redirect so that URL in browser also changes return HttpResponseRedirect( reverse('view_trip', kwargs={ 'username': username, 'trip_name_slug': trip_name_slug })) else: print(form.errors) return render(request, 'app/add_blog_post.html', { 'form': form, 'slug': trip_name_slug })
def add_blog(): form = BlogForm() return render_template('add_blog.html', title='Create Blog', form=form, root=root)