Load a variable from a file with module. Django/Python

Asked

Viewed 181 times

-2

Hello, my name is Marvin, I’m a beginner in the area and I have a problem giving a 'POST' a 'file.py' variable to my local server Django. I use a module called 'openpyxl' that serves to load a table from an excel(.XLS) and save all the information within a vector.
My code works perfectly, I checked if the module is already installed and I researched a lot about but I could not find the solution. I use the version of Django 1.11.17 and the latest in python

This is the error I have in the command prompt:

inserir a descrição da imagem aqui


My code in python is this:

´´´´
from openpyxl import load_workbook

bd1 = list(range(1, 300))

wb = load_workbook('BD2.xlsx')


sh = wb['Planilha1']


coluns = sh.max_column
rows = sh.max_row


word = input('Search: ')


for i in range(1, rows + 1):
    for j in range(1, coluns + 1):
        c = sh.cell(i, j)

        if str(c.value).lower().find(word.lower()) != -1:
            cont = 0
            cont = cont + 1
            for x in range(1, coluns + 1):
                bd1[cont] = sh.cell(i, x).value
                print(bd1[cont])




My view where he can’t read my.py file.':


from django.shortcuts import render
from .Excel import bd1
from .Excel2 import bd2


# Create your views here.

def homepage(request):
    return render(request, 'bin/index.html', {'cont2': bd1, 'cont': bd2})
´´´´´´




Settings.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'app',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'server.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'server.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'

Thank you for your attention and thank you in advance! https://github.com/marvinbraescher/Excelfile

1 answer

0

From what I understand (if it is not correct tell me), The code that imports openpyxl ,in it there is an input function, will be a search in the file BD2.xlsx, when calling this module will be made a search for some information.

This module was imported to the views on Jango, msmo does not find this module and its template is not completed.

Django in this code will not interact with the input in the other module.

It could be so.

Pick a field in html "search" and this variable can be passed to xls instead of "word = input('Search: ')".

Example

def searchxls(request):

form = searchForms(request.POST or None)
if form.is_valid():
    det = form.data
    search= det["search"]
    print("Item pesquisado %s"%search)
     .....Inicio código xls
    #word = input('Search: ') trocar por...
    word = search
     ...Fim do código xls
      passando o resultado para um dic e direcionado para o render

On html page a loop (for) show all results

Browser other questions tagged

You are not signed in. Login or sign up in order to post.