def create(request): if request.method == 'POST': if request.POST.get('title') and request.POST.get('body') and request.POST.get('url') \ and request.FILES.get('icon') and request.FILES.get('image'): request_url = request.POST.get('url') product = Product() product.title = request.POST.get('title') product.body = request.POST.get('body') if request_url.startswith('http://') or request_url.startswith( 'https://'): product.url = request_url else: product.url = f'http://{request_url}' product.image = request.FILES.get('image') product.icon = request.FILES.get('icon') product.pub_date = timezone.datetime.now() product.hunter = request.user product.save() return redirect(f'/products/{product.id}') else: return render(request, 'products/create.html', {'error': 'All fields are required'}) else: return render(request, 'products/create.html')
def create(request): error = 'Have Some Error ' context = { 'error': error } if request.method == "POST": if request.POST['title'] and request.POST['body'] and request.POST['urls'] and request.FILES['image'] and \ request.FILES['icon']: # create Product Objects product = Product() product.title = request.POST['title'] product.body = request.POST['body'] if request.POST['urls'].startswith('http://') or request.POST['urls'].startswith('https://'): product.url = request.POST['urls'] else: product.url = 'http://' + request.POST['urls'] product.icon = request.FILES['icon'] product.image = request.FILES['image'] product.pub_date = timezone.datetime.now() product.hunter = request.user product.save() return redirect("home") else: return render(request, 'products/create.html', context) else: return render(request, 'products/create.html')
def create(request): if request.method == 'POST': if request.POST['title'] and request.POST['body'] and request.POST[ 'url'] and request.FILES['icon'] and request.FILES['image']: product = Product() product.title = request.POST['title'] product.body = request.POST['body'] if request.POST['url'].startswith( 'http://') or request.POST['url'].startswith('https://'): product.url = request.POST['url'] else: product.url = 'http://' + request.POST['url'] try: product.icon = request.FILES['icon'] product.image = request.FILES['image'] except Exception as error: return render(request, 'products/create.html', {'error': 'icon or image is missing'}) product.pub_date = timezone.datetime.now() product.hunter = request.user product.save() return redirect('/products' + str(product.id)) else: return render(request, 'products/create.html', {'error': 'all fields are required.'}) else: return render(request, 'products/create.html')
def create_product(request): if request.method == 'POST': if request.POST['title'] and request.POST['description'] and request.POST['url'] and request.FILES['image'] and request.FILES['icon']: product = Product() product.title = request.POST['title'] product.body = request.POST['description'] product.url = request.POST['url'] product.pub_date = datetime.now() product.image = request.FILES['image'] product.icon = request.FILES['icon'] product.hunter = request.user product.save() return redirect(f'/products/{product.id}') else: return render(request, 'products/create_product.html', {'error': 'All fields are required'}) else: return render(request, 'products/create_product.html')