def adminside(request): user=request.user if(user.is_superuser): if(request.method=='POST'): title=request.POST['title'] author= request.POST['author'] slug=request.POST['slug'] content=request.POST['content'] post = Post(title=title,author=author,slug=slug,content=content) post.save() messages.success(request,"Your Post is Published Successfully.") return redirect('home') return render(request,'mysite/writing.html') else: messages.error(request, "You are not authorised to visit this page") return redirect('home')
def post(request): form = ImageUploadForm(request.POST, request.FILES) username = request.user.username if form.is_valid(): photo = form.cleaned_data['photo'] else: photo = 'empty' text = request.POST.get('text', 'empty') expiration = request.POST.get('expiration', 'empty') post = Post(username=username, photo=photo, text=text, expiration=expiration) try: post.save() except(DataError): return render(request, 'post.html', { 'error_message': "Posts must be 400 characters or less!"}) else: return HttpResponseRedirect(reverse('index'))
def post(): form = Postform() if form.validate_on_submit(): p_post = Post(title=form.title.data, post=form.content.data, author=current_user) db.session.add(p_post) db.session.commit() flash("ur post has been created ", "success") return redirect(url_for('home')) return render_template("post.html", title="New Post", form=form)
def content_html(self): from mysite.models import Post from comment.models import Comment result = '' if self.display_type == self.DISPLAY_HTML: result = self.content elif self.display_type == self.DISPLAY_LATEST: context = {'posts': Post.latest_posts()} result = render_to_string('config/block/sidebar_posts.html', context) elif self.display_type == self.DISPLAY_HOT: context = {'posts': Post.hot_posts()} result = render_to_string('config/block/sidebar_posts.html', context) elif self.display_type == self.DISPLAY_COMMENT: context = { 'comments': Comment.objects.filter(status=Comment.STATUS_NORMAL) } result = render_to_string('config/block/sidebar_posts.html', context) return result
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('O seu post foi criado!', 'success') return redirect(url_for('home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def posts(): page = request.args.get('page', 1, type=int) p_posts = Post.query.order_by(Post.date.desc()).paginate(page=page, per_page=2) form = Postform() if form.validate_on_submit(): p_post = Post(title=form.title.data, post=form.content.data, author=current_user) db.session.add(p_post) db.session.commit() flash("ur post has been created ", "success") return redirect(url_for('posts')) return render_template('posts.html', posts=p_posts, form=form)
import os import django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'datacenter.settings') django.setup() from mysite.models import Post for i in range(10): newdata = Post(title="我的標題{}".format(i + 3), body="我的內文{}".format(i + 3)) newdata.save()
import csv import os import django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'datacenter.settings') django.setup() from mysite.models import Post with open("recycle1.csv", newline="\n", encoding="utf-8") as csvfile: rows = csv.reader(csvfile, delimiter=",") for row in rows: print(row) newdata = Post(title =str(row[1]),body =str(row[2])) newdata.save()