I explain my problem as better as I can:
I have a program written in 3 modules. I want to save some data from a site (Now I'm using a local host to make tests) to some variables and show some variables' value in the index page in this way:
1.- [Script: Text_Variable] <-- [localhost: ask_for_text]
2.- [Script: Menu_Options] --> [localhost: show_menu]
3.- [Script: Option_Variable] <-- [localhost: user_option]
4.- [Script: Response_Variable] --> [localhost: show_response]
Depending the value of "Text_Variable", the program do: 1 --> 4 or 1 --> 2 --> 3 --> 4 I could import from the script in "views.py" without problems functions and variables which made the process 1 --> 4 work properly but I have a problem with the function "Menu_Options" : I use "POST" method for getting the data from the page, so I need to pass the argument "request" to my function in "views.py" but I call this function from the script, so I need to import that "request" in the module. I tried with these:
import newproject.views
import django
from django.http import request
script_module.py:
...
Pregunta = 'question?'
Opciones = [op1, op2...]
opc = boletin.views.Menu_Opciones(request, Pregunta, Opciones)
...
views.py:
...
def Menu_Opciones(request, Pregunta, Opciones):
request.session['ops'] = Opciones
request.session['preg'] = Pregunta
Menu_Opc(request)
opcion = request.session['op']
return opcion
...
...
def Menu_Opc(request):
Opciones = request.session['ops']
Pregunta = request.session['preg']
form = OpcionesForm(request.POST or None)
form.Campo_Opciones.widget.choices = Opciones
context = {
'pregunta': Pregunta,
'form': form,
}
if form.is_valid():
opcion = form.instance.ops.value() + 1
request.session['op'] = opcion
return render(request, "inicio.html", context)
...
but I had this error: "Menu_Opciones() missing 1 required positional argument: 'Opciones'"
Here is the traceback:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/
Django Version: 1.9.8
Python Version: 3.4.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'boletin',
'proyectoapp']
Installed 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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\ATI\Desktop\probardjango\lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "C:\Users\ATI\Desktop\probardjango\lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\ATI\Desktop\probardjango\src\boletin\views.py" in inicio
42. salida = main(pregunta)
File "C:\Users\ATI\Proyecto\ESI\Main.py" in main
52. Respuesta = construir_respuesta(pregunta_lista, 'Profesores', CSin, TMaxCoin)
File "C:\Users\ATI\Proyecto\ESI\Entrada_Salida.py" in construir_respuesta
127. opc = boletin.views.Menu_Opciones(Pregunta, Opciones)
Exception Type: TypeError at /
Exception Value: Menu_Opciones() missing 1 required positional argument: 'Opciones'
Am I doing something wrong this way or there are a better way to do this? Please, consider I'm new in this language. Thanks in advance.
EDIT:
I fixed one problem! I tell you how:
First I added my python modules' path to setting.py, then I decided to copy all scripts modules and paste inside my django project but forgot to change the path... Then, everytime I edited my module, I really edited the wrong file... that's why the traceback seems to have no sense.
But now I'm getting a new error:
"'module' object has no attribute 'session'"
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/
Django Version: 1.9.8
Python Version: 3.4.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'boletin',
'proyectoapp']
Installed 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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\ATI\Desktop\probardjango\lib\site- packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "C:\Users\ATI\Desktop\probardjango\lib\site- packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\ATI\Desktop\probardjango\src\boletin\views.py" in inicio
42. salida = main(pregunta)
File "C:\Users\ATI\Desktop\probardjango\src\boletin\Main.py" in main
52. Respuesta = construir_respuesta(pregunta_lista, 'Profesores', CSin, TMaxCoin)
File "C:\Users\ATI\Desktop\probardjango\src\boletin\Entrada_Salida.py" in construir_respuesta
127. opc = boletin.views.Menu_Opciones(request, Pregunta, Opciones)
File "C:\Users\ATI\Desktop\probardjango\src\boletin\views.py" in Menu_Opciones
137. request.session['ops'] = Opciones
Exception Type: AttributeError at /
Exception Value: 'module' object has no attribute 'session'
if I change the import code in this way:
from django.http import request
to:
from django.http import HttpRequest
request = HttpRequest()
I have this other error:
"'HttpRequest' object has no attribute 'session'"
and the traceback is exactly equal but that error.
Is not "request" and object of "HttpRequest"? Is not "HttpRequest" inside "django.http"? Has not an object of "HttpRequest" an attribute 'session'? What am I doing wrong?