Created
January 2, 2018 18:58
-
-
Save SamsadSajid/3b30bee6d8af9f164bd8b6a763476412 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_product(request): | |
user = request.user | |
product_list = ProductList() | |
if request.method == 'POST': | |
print ('yoo') | |
form = ProductForm(request.POST, request.FILES) | |
print ('yoo') | |
print (form) | |
if form.is_valid(): | |
print ('yoo') | |
product_list.product_id = form.cleaned_data.get('product_id') | |
print (product_list.product_id) | |
product_list.product_name = form.cleaned_data.get('product_name') | |
product_list.uploaded_by = user | |
product_list.quantity = form.cleaned_data.get('quantity') | |
product_list.product_available = form.cleaned_data.get('product_available') | |
product_list.product_description = form.cleaned_data.get('product_description') | |
product_list.slug = product_list.product_name.replace(' ', '-') | |
product_list.product_image = request.FILES['product_image'] | |
file_type = product_list.product_image.url.split('.')[-1] | |
file_type = file_type.lower() | |
product_list.save() | |
# product = form.save(commit=False) | |
# product.uploaded_by = user | |
# product.product_image = request.FILES['product_image'] | |
# form.save() | |
# return render(request, 'dashboard/all_product.html', {'form': form}) | |
return redirect('/officer/all_products/') | |
else: | |
form = ProductForm() | |
print (form.errors) | |
messages.add_message(request, | |
messages.ERROR, | |
"Submitted form is not valid! Try again.") | |
return render(request, 'dashboard/add_products.html', {'form': form}) | |
else: | |
form = ProductForm(instance=product_list, initial={ | |
'product_id': product_list.product_id, | |
'product_name': product_list.product_name, | |
'product_image': product_list.product_image, | |
'quantity': product_list.quantity, | |
'product_available': product_list.product_available, | |
'product_description': product_list.product_description | |
}) | |
return render(request, 'dashboard/add_products.html', {'form': form}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment