I have a view in Django and I have to call another function in this view. When i call the function in the view, the i got 'str' object has no attribute 'user'
error. But when i delete the function call, there is no error. It gave the error in django contrib decorator such as below.
C:\Users\imgea\Anaconda3\lib\site-packages\django\contrib\auth\decorators.py in _wrapped_view
redirecting to the log-in page if necessary. The test should be a callable
that takes the user object and returns True if the user passes.
"""
def decorator(view_func):
@wraps(view_func)
def _wrapped_view(request, *args, **kwargs):
if test_func(request.user):
return view_func(request, *args, **kwargs)
path = request.build_absolute_uri()
resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
My view;
def optimize(request):
customer_name = str(request.user)
model_name = customer_name + "_cost"
nominals = ['material_type','program','isleme_merkezi', 'baglama_sekil', 'ymmm', 'tezgah_kodu']
cost_id = request.session['opt_id']
optimization_features = request.session['opt_features']
cost = Cost.objects.get(id=cost_id)
cost_dict = model_to_dict(cost)
features = request.user.profile.selected_features
unknown_nominal_column_names = []
unknown_column_names = []
unknown_initial_guess = []
known_map = dict()
for feature in features:
known_map[feature] = cost_dict[feature]
for feature in optimization_features:
if feature in nominals:
unknown_nominal_column_names.append(feature)
else:
unknown_column_names.append(feature)
unknown_initial_guess.append(float(known_map[feature]))
values, optimize_cost = optimize_parameters(customer_name, model_name, known_map, unknown_column_names, unknown_nominal_column_names, unknown_initial_guess)
# print(values)
# print(optimize_cost)
return render(request, "optimize_result.html")
My function which i want to call in that view;
def optimize_parameters(customer_name, model_name, known_values, unknown_column_names, unknown_nominal_column_names, unknown_initial_guess):
models = Prediction.load_models(customer_name, model_name)
if unknown_column_names:
bounds = (75, 100)
else:
bounds = (None, None)
single_model = models[0][0]
unknown_nominal_column_indices = [single_model.nominal_columns.index(x) for x in unknown_nominal_column_names]
unknown_nominal_values = [single_model.encoder.categories_[i] for i in unknown_nominal_column_indices]
# create lambda function
objective_function = lambda x, unknown_nominal_dictionary: Prediction.optimize_prediction(x,
unknown_column_names,
unknown_nominal_dictionary,
known_values, models,
0.7)
lowest_values, lowest_cost = optimize(objective_function, bounds, unknown_column_names, unknown_nominal_column_names, unknown_nominal_values)
return lowest_values, lowest_cost
I saw a solution for this problem which is that deleting the login_required
but I still get the same error even though I tried this. How can i fix the problem?