def add_category(): py = Category("Python") p = Post("hello python!", "python is pretty cool ", py) py.save() p.save() return "add category success"
def publish_article(request, cid=-1): if request.method == "POST": data = request.POST.dict() data.pop('csrfmiddlewaretoken') # data.pop('photo') file = request.FILES.get('photo', '') data.pop('photo') if file: savePath = os.path.join(settings.MDEIA_ROOT, file.name) print(savePath) with open(savePath, 'wb') as f: if file.multiple_chunks(): for myf in file.chunks(): f.write(myf) print('大于2.5') else: f.write(file.read()) print('小于2.5') data['picture'] = "/upload/" + file.name data['create_time'] = datetime.now() print(data, type(data)) Article.add(**data) # return HttpResponse('文件上传成功') return redirect(reverse("App:wenzhang_xinwen")) else: cid = cid if cid > 0 else Category.first_category().cid return render(request, 'wenzhang_xinwen_fabu.html', locals())
def add_category(): if not current_user.isAdmin: abort(403) form = request.form cat = Category(name=form['name'], picture=save_picture(request.files['picture'], 'category')) db.session.add(cat) db.session.commit() flash(f'Category added Successfully', 'success') return redirect(url_for('admin_categories'))
def fenlei_xinwen_fabu(request): if request.session.get('username'): categories = Category.objects.all() if request.method == 'POST': category = request.POST.get('category') for cate in categories: if category == cate.c_name: messages.add_message(request, messages.ERROR, '分类重复', extra_tags='alert alert-danger') return render(request, 'fenlei_xinwen_fabu.html', locals()) else: cate = Category(c_name=category) cate.save() return redirect(reverse('App:left_cate')) else: return redirect(reverse('App:login')) return render(request, 'fenlei_xinwen_fabu.html', locals())
def publish_article(request, cid=-1): if request.method == "POST": data = request.POST.dict() data.pop('csrfmiddlewaretoken') data.pop('photo') Article.add(**data) return redirect(reverse("App:main")) else: cid = cid if cid > 0 else Category.first_category().cid return render(request, 'wenzhang_xinwen_fabu.html', locals())
def pub_category(request): if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): name = request.POST.get('name') num = request.POST.get('num') categories = Category.objects.all() # print(categories) for data in categories: if data.name == name: messages.error(request, '该类别已存在,请重新输入') return render(request, 'category_fabu.html') else: category = Category(name=name, num=num) category.save() return redirect(reverse('App03:catg')) else: return render(request, 'category_fabu.html', locals()) else: return render(request, 'category_fabu.html')
def fabu(request, cid=-1): if request.method == 'POST': fobj = request.FILES.get('picture') path = settings.MDEIA_ROOT fp = FileUpload(fobj) # print(fp) #<App02.utils.FileUpload object at 0x00000000048BA4A8> # print(fobj.name) #2.png form = ArticleForm(request.POST) if form.is_valid() and fp.upload(path): data = request.POST.dict() print(data) data.pop('csrfmiddlewaretoken') data['picture'] = 'upload/' + fobj.name # print(data) Article.add(**data) messages.success(request, '发表成功') return redirect(reverse('App:main')) # return HttpResponse('ok') else: messages.error(request, '文件上传有误,请重新上传') return render(request, 'wenzhang_xinwen_fabu.html', locals()) else: cid = cid if cid > 0 else Category.first_category().cid return render(request, 'wenzhang_xinwen_fabu.html', locals())
from App import db from App.models import User, Stock, Request, Category db.create_all() categories = [ Category( id=1, name='Paper Products', ), Category(id=2, name='Writing Material'), Category(id=3, name='Desk Item'), Category(id=4, name='File / Folder'), Category(id=5, name='Art / Crafts'), Category(id=6, name='Cleaning Material'), Category(id=7, name='Coputer Pheripherals'), ] stocks = [ Stock( item='Long Register (140 Pages)', category_id=1, qty_prev=12, avail=5, qty_req=0, qty_pres=0, maximum_limit=3, minimum_limit=2, quota=20, ), Stock(
def print_repr(): c = Category("java") print(c) print(type(c)) return "print __repr__ : {} success".format(c)