本文整理汇总了Python中rango.forms.CategoryForm类的典型用法代码示例。如果您正苦于以下问题:Python CategoryForm类的具体用法?Python CategoryForm怎么用?Python CategoryForm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CategoryForm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_blank_data
def test_blank_data(self):
"""
Checks form with blank data and corresponding form errors.
"""
form = CategoryForm(data={})
self.assertFalse(form.is_valid())
self.assertEqual(form.errors, {'name': ['This field is required.']})
示例2: add_category
def add_category(request):
# Get the context
context = RequestContext(request)
# A HTTP POST?
if request.method == 'POST':
form = CategoryForm(request.POST)
# have we been provided with a valud form
if form.is_valid():
# Save the new category to the database
form.save(commit=True)
# redirect to homepage
return index(request)
else:
print form.errors
else:
# If the request was not a POST, display the form to enter details.
form = CategoryForm()
context_dict = {'form' : form}
context_dict['cat_list'] = get_cat_list()
# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return render_to_response('rango/add_category.html', context_dict, context)
示例3: add_category
def add_category(request):
form = CategoryForm()
# 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.
form.save(commit=True)
# Now that the category is saved
# We could give a confirmation message
# But since the most recent category added is on the index page
# then we can redirect the user back to the index page.
return index(request)
else:
# The supplied form contained errors -
# just print them to the terminal.
print(form.errors)
# Will handle the bad form, new form or
# no form supplied cases.
# Render the form with error messages (if any).
return render(request, 'rango/add_category.html', {'form': form})
示例4: add_category
def add_category(request):
# get context from request
context = RequestContext(request)
if request.method == 'POST':
form = CategoryForm(request.POST)
# check if form is valid
if form.is_valid():
# save new category to db
form.save(commit=True)
# now call the index() view
# the user will be shown the homepage
return index(request)
else:
# form had errors
print form.errors
else:
# non-POST requests should display form to user
form = CategoryForm()
# bad form (or form details), no form supplied...
# render form with error messages (if any)
return render_to_response(
'rango/add_category.html', {'form': form}, context)
示例5: add_category
def add_category(request):
# Get the context from the request.
context = RequestContext(request)
cat_list = get_category_list()
# 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.
form.save(commit=True)
# Now call the index() view.
# The user will be shown the homepage.
return index(request)
else:
# The supplied form contained errors - just print them to the terminal.
print form.errors
# According to REST principles, if we didn't use POST, it has to be a GET, then display a form to submit it afterwards.
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, 'cat_list': cat_list}, context)
示例6: add_category
def add_category(request):
"""
User must be logged in to use this functionality.
Adds a new category corresponding to the information in the form filled
by the user. If form empty, display form. If form filled and valid, create
a new category. If form filled and not valid, display form errors.
"""
# An 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.
form.save(commit=True)
# Now call the index() view.
# The user will be shown the homepage.
return index(request)
else:
# The supplied form contained errors - just print them to the terminal.
#return HttpResponse(form.errors)
#return index(request)
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(request, 'rango/add_category.html', {'form': form})
示例7: add_category
def add_category(request):
context = RequestContext(request)
#Checking whether the submit type is POST or not (becauase the form is submitted on POST)
if request.method == 'POST':
#Process the form data
form = CategoryForm(request.POST)
#Check the validity
if form.is_valid():
#Save the new category to our SQLite 3 Database
form.save(commit=True)
#Now load it up on the Index view as the new Category is saved...Easy!
return index(request)
else:
#if containingf errors, print it out to the terminal )(will replace after dev)
print form.errors
else:
#If not post , then display the form to the user.....(As both Post and get are dpne to the same thread unlike PHP)
form = CategoryForm()
#Invoked only when NOT SUCCESS
return render_to_response('rango/add_category.html' , {"form" :form} , context)
示例8: add_category
def add_category(request):
context = RequestContext(request)
#Check the html message type
if request.method == 'POST' :
form = CategoryForm(request.POST)
#Validate the form
if form.is_valid():
#Save the new object
form.save(commit=True)
#Redirect the user to the index
return index(request)
else:
#Print errors on terminal
print form.errors
else:
#Get the form attributes
form = CategoryForm()
#Bad Form (or form details)
#Render the form with error messages
return render_to_response('rango/add_category.html',
{
'form':form,
'cat_list': get_category_list()
},
context)
示例9: add_category
def add_category(request):
#get the context form the request
context = RequestContext(request)
#A HTTP POST?
if request.method == 'POST':
form = CategoryForm(request.POST)
#have them provided with a valid form
if form.is_valid():
form.save(commit=True)
#now call the main() view
#the user will be shown the homepage
return main(request)
else:
print form.errors
else:
form = CategoryForm()
#bad form of form details, no from suppied
#render the form with error msgs if any
return render_to_response('rango/add_category.html', {'form':form}, context)
示例10: add_category
def add_category(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.
form.save(commit=True)
# Now call the index() view.
# The user will be shown the homepage.
return index(request)
else:
# If the supplied form contained errors, making it NOT valid, print these
# errors to the terminal
print form.errors
else:
# If the request was not a POST, display the form to enter details
# (i.e. upon first load)
form = CategoryForm()
# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return render(request, 'rango/add_category.html', {'form': form})
示例11: add_category
def add_category(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.
cat = form.save(commit=True)
print(cat, cat.slug)
# Now call the index() view.
# The user will be shown the homepage.
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()
context_dict = {'form': form}
# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return render(request, 'rango/add_category.html', context_dict)
示例12: add_category
def add_category(request):
#get the context
context = RequestContext(request)
#create empty error dictionary
errors = ''
#check if POST
if request.method == 'POST':
form = CategoryForm(request.POST)
#check if form is valid
if form.is_valid():
#save the new category to the database
form.save(commit=True)
#go back to the main page
return index(request)
else:
# form contains some error extract them
errors = form.errors
else:
# request is GET simply display the form to the user
form = CategoryForm()
#create dictionary with site context
context_dict = {'form' : form,
'errors' : errors,
'categories' : get_category_list()}
return render_to_response('add_category.html',context_dict, context)
示例13: add_category
def add_category(req):
"""View used to add new categories"""
# Get the context from the request.
context = RequestContext(req)
# Retrieve categories list for the left navbar
cat_list = get_category_list()
# A HTTP POST? If so, provide a form for posting a new category
if req.method == 'POST':
form = CategoryForm(req.POST)
# Have we been provided with a valid form input from the user?
if form.is_valid():
# Save the new category to the database
form.save(commit=True)
# Now call the index() view.
# The user will be served with the index.html template
return index(req)
else:
# The supplied form contained errors - just print them to the terminal
print form.errors
else:
# If the request wasn't a POST, display the form to enter details
form = CategoryForm()
context_dict = {'cat_list': cat_list, 'form': form}
# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return render_to_response('rango/add_category.html', context_dict, context)
示例14: add_category
def add_category(request):
# HTTP Post
if request.method == 'POST':
form = CategoryForm(request.POST)
# Is form valid?
if form.is_valid():
# Save category to database
form.save(commit=True)
# Call index and go to homepage
return index(request)
# If not valid form
else:
# Form errors (printed to terminal)
print(form.errors)
# HTTP Get
elif request.method == 'GET':
# Create form
form = CategoryForm
# Render form - include any error messages
return render(request, 'rango/add_category.html', {'form': form})
示例15: add_category
def add_category(request):
context_dict = {}
print('[debug] what call is this? {}'.format(request.method))
# A HTTP POST?
if request.method == 'POST':
form = CategoryForm(request.POST)
# Provided with a valid form?
if form.is_valid():
# Save the new category to the database
cat = form.save(commit=True)
print('[debug] {} - {}'.format(cat, cat.slug))
# Now call the index() view.
# The user will be shown the homepage.
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 (on submit), display the form to enter details.
form = CategoryForm()
# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
context_dict['form'] = form
return render(request, 'rango/add_category.html', context_dict)