本文整理汇总了Python中forms.CategoryForm类的典型用法代码示例。如果您正苦于以下问题:Python CategoryForm类的具体用法?Python CategoryForm怎么用?Python CategoryForm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CategoryForm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: index
def index(request):
args = {}
if request.method == 'POST':
form = CategoryForm(request.POST)
if form.is_valid():
# Passes error through if user selected more than three category ids
category_num = len(form.cleaned_data['category_ids'])
if category_num > 3:
return HttpResponseRedirect('/?too_many_categories=true')
if category_num == 0:
return HttpResponseRedirect('/?too_few_categories=true')
event_search_url = "?category_ids=" + ",".join(form.cleaned_data['category_ids'])
event_search_url += "&location=" + form.cleaned_data['location']
return HttpResponseRedirect('/events/' + event_search_url)
else:
return HttpResponseRedirect('/?no_location=true')
else:
# Raises an error if user has not selected any categories
if request.GET.get('too_few_categories'):
args['error'] = "You must select at least 1 category"
# Raises an error if user selected more than three category ids
if request.GET.get('too_many_categories'):
args['error'] = "You may select no more than 3 categories"
if request.GET.get('no_location'):
args['error'] = "You must include a location"
# Form will initialize with valid categories from eventbrite API
args['category_form'] = CategoryForm()
return render(request, 'index.html', args)
示例2: put
def put(self, id):
category = Category.get_by_id(id)
if category is None:
flash(gettext('The category was not found'), 'error')
return redirect(url_for('CategoriesView:index'))
if not category.can_edit():
abort(401)
if request.method in ['POST', 'PUT']:
form = CategoryForm()
if form.validate_on_submit():
try:
form.populate_obj(category)
category.save()
flash(gettext('Category was succesfully saved'))
return util.redirect_json_or_html(url_for('CategoriesView:index'), 'category')
except:
return util.redirect_json_or_html(url_for('CategoriesView:index'), 'category', gettext('Error while updating the category'))
else:
if request.is_xhr:
return util.redirect_json_or_html(url_for('CategoriesView:index'), 'category', gettext('Invalid submission, please check the message below'))
else:
flash(gettext('Invalid submission, please check the message below'), 'error')
else:
form = NewCategoryForm(category)
return render_template('admin/categories/edit.html',
title=gettext('Edit Category: %(name)s', name=category.name),
form=form,
category=category)
示例3: add_category
def add_category(request):
# Get the context from the request.
context = RequestContext(request)
# A HTTP POST?
if request.method == 'POST':
form = CategoryForm(request.POST)
# Have we been provided with a valid form?
if form.is_valid():
# Save the new category to the database.
category = form.save(commit=True)
# Now call the index() view.
# The user will be shown the homepage.
action.send(request.user, verb='added', action_object=category)
return index(request)
else:
# The supplied form contained errors - just print them to the terminal.
print form.errors
else:
# If the request was not a POST, display the form to enter details.
form = CategoryForm()
# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return render_to_response('rango/add_category.html', {'form': form}, context)
示例4: add_category
def add_category(request):
"""
Either show a form or create a new instance of one.
"""
# Is request HTTP POST or GET?
if request.method == "POST":
form = CategoryForm(request.POST)
if form.is_valid():
category = form.save(commit=False)
setattr(category,"url", Category._encode_url_name(category.name))
category.save()
# now call the index() view
# user redirected to home page
return index(request)
else:
# Uh oh, supplied form contains error - print out to console
print form.errors
else:
# request not a POST, display the form to enter details
form = CategoryForm()
# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return _process_request(request, {"form": form}, "rango/add_category.html")
示例5: editCategory
def editCategory(name):
"""Edit a named category or render a form.
Args: A category name
Returns: Edited category or render a form.
"""
# Store caetgory and logged in user
category = Category.query.filter_by(name=name).first_or_404()
user = Users.query.filter_by(id=login_session["users_id"]).first_or_404()
# Verify that the logged in user is creator or admin
if category.users_id != login_session["users_id"] and not user.admin:
flash(" You are not authorized to make that edit.")
return redirect(url_for("category.showCategory", category_id=category_id))
# Initiate the form.
form = CategoryForm(obj=category)
# On POST of a valid form, edit the category.
if form.validate_on_submit():
category.name = form.name.data
category.description = form.description.data
category.picture = form.picture.data
db.session.add(category)
db.session.commit()
flash(" Category %s Successfully Edited" % category.name)
return redirect(url_for("category.showCategory", name=category.name))
else:
return render_template("editCategory.html", form=form, category=category)
示例6: category_update
def category_update(category_name):
"""Update category name and description in database"""
if 'username' not in login_session:
return redirect(url_for('login'))
category = Category.query.filter(Category.name == category_name).first()
if category.author_id != login_session['user_id']:
return ""
form = CategoryForm()
if form.validate_on_submit():
try:
category.name = form.name.data
category.description = form.description.data
db_session.add(category)
db_session.commit()
return redirect(url_for('category_view',
category_name=form.name.data))
except IntegrityError:
db_session.rollback()
flash("Category name must be unique value")
except Exception:
db_session.rollback()
flash("Database error encountered")
flash_errors(form)
return redirect(url_for('category_edit',
category_name=category_name))
示例7: post
def post(self):
form = CategoryForm()
if form.is_submitted():
try:
if not form.validate():
raise Exception(_('ERROR_INVALID_SUBMISSION'))
category = Category.create()
if not form.slug.data:
form.slug.data = form.name.data
form.slug.data = Category.urlify(form.slug.data)
form.populate_obj(category)
category.save()
return render_view(url_for('CategoriesView:put', id=category.id),
message=_('CATEGORY_CREATE_SUCESS'),
redirect=True)
except Exception as e:
flash(e.message, 'error')
return render_view('admin/categories/add.html', form=form)
示例8: cat_modify_post
def cat_modify_post(request, id='0', cxt={}):
assert request.method == 'POST'
cxt.update(csrf(request))
category = Category.objects.get(id=int(id))
initial = {
'name': category.name,
'color': category.color,
'public': category.public
}
category_form = CategoryForm(request.POST, initial=initial)
cxt.update({'category_form': category_form, 'category': category})
if category_form.is_valid():
data = category_form.cleaned_data
category_name = data['name']
if 'name' in category_form.changed_data:
if Category.objects.filter(name=category_name):
category_form.add_error('name', 'Duplicated Category Name')
else:
Category.objects.filter(id=int(id)).update(
name=data['name'],
color='#' + data['color'][-6:],
public=data['public']
)
else:
Category.objects.filter(id=int(id)).update(
color='#' + data['color'][-6:],
public=data['public']
)
return render_to_response('category_modify.html', cxt)
示例9: addcategory
def addcategory():
form = CategoryForm()
if form.validate_on_submit():
category = Category(name=form.name.data,)
db.session.add(category)
db.session.commit()
return redirect(url_for('admin.listcategory'))
return render_template('/addcategory.html', form=form)
示例10: editCategory
def editCategory(cat_id):
catForm = CategoryForm()
cat = Category.query.get(cat_id)
if catForm.validate_on_submit():
cat.name = catForm.name.data
cat.note=catForm.note.data
db.session.commit()
return redirect(url_for('shopapp.shop_main'))
示例11: editcategory
def editcategory():
form = CategoryForm()
form.name.data = request.args.get('category_id')
if form.validate_on_submit():
category = Category.query.filter_by(name=form.name.data).first()
category.name = form.name.data
db.session.add(category)
db.session.commit()
return redirect(url_for('admin.listcategory'))
return render_template('/addcategory.html', form=form)
示例12: category_edit
def category_edit(request, id):
category = get_object_or_404(Category, id=id)
category_titles = CategoryTitle.objects.filter(category=category)
category_titles_langs = {}
for lang in settings.LANGUAGES:
category_titles_langs[lang] = None
for category_title in category_titles:
category_titles_langs[category_title.lang] = category_title
if request.method == 'POST':
category_form = CategoryForm(request.POST,prefix='category_form', instance=category)
category_title_forms = []
for lang in settings.LANGUAGES:
category_title_forms.append({
'form':CategoryTitleForm(request.POST,prefix='category_title_' + lang[0]),
'lang':lang[0]
})
if category_form.is_valid():
category = category_form.save(commit=False)
category.parent = parent
category.save()
valid = False
for category_title_form in category_title_forms:
valid = category_title_form['form'].is_valid()
if not valid:
break
# print 'valid', valid
if valid:
for category_title_form in category_title_forms:
category_title = category_title_form['form'].save(commit=False)
category_title.lang = category_title_form['lang']
category_title.category = category
category_title.save()
return redirect('ask_librarian:administration:categories_list')
else:
category_form = CategoryForm(prefix="category_form")
category_title_forms = []
for lang in settings.LANGUAGES:
category_title_forms.append({
'form':CategoryTitleForm(prefix='category_title_' + lang[0]),
'lang':lang[0]
})
return render(request, 'ask_librarian/administration/create_category.html', {
'category_form': category_form,
'category_title_forms': category_title_forms,
})
示例13: newCat
def newCat():
catForm = CategoryForm()
name = catForm.name.data
if catForm.validate_on_submit():
if Category.query.filter_by(name=name).first():
flash('Category already exists!','alert-danger')
else:
cat = Category(name=name, note = catForm.note.data)
db.session.add(cat)
db.session.commit()
return redirect(url_for('shopapp.shop_main'))
示例14: add_category
def add_category(request):
if request.method == 'POST':
form = CategoryForm(request.POST)
if form.is_valid():
form.save(commit=True)
return index(request)
else:
print form.errors
else:
form = CategoryForm()
return render(request, 'rango/add_category.html', {'form': form})
示例15: admin_category
def admin_category():
form = CategoryForm()
if request.method == 'POST' and form.validate_on_submit():
cat_name = form.cat_name.data.strip()
cat_description = form.cat_description.data.strip()
Category.save(cat_name, cat_description)
categories = Category.query.all()
return render_template('admin_categories.html', title='Admin', user=current_user, categories=categories)
return render_template('admin_new_category.html', title='Admin', form=form, user=current_user)