0

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?

Aslı Kök
  • 616
  • 8
  • 19
  • I have added them. – Aslı Kök May 12 '20 at 16:21
  • Where did you use `login_required`? – Alasdair May 12 '20 at 16:26
  • I used in the beginning of the optimize view but i deleted. – Aslı Kök May 12 '20 at 16:27
  • My guess is that there is a function name clash between your `optimize` view and the `optimize` function that you called inside `optimize_parameters`. It should be giving an positional arguments error since the view expects only one argument, but it's the only way I can think of that the `request` parameter will be treated as a string. – Eduardo macedo May 12 '20 at 16:48
  • Once you remove `login_required`, save your changes, and restart the server, you should get a different error. Please show that in your question as well. – Alasdair May 12 '20 at 16:54
  • Your `optimize_parameters` fucntion calls `lowest_values, lowest_cost = optimize(...)` - it looks as if there is a name clash between that `optimize` function and the `optimize` view. Try renaming one of them. – Alasdair May 12 '20 at 16:55
  • Yes, i should. But i did not get a different error. I got the same error in both situation. – Aslı Kök May 12 '20 at 16:56
  • I have tried changing the name, i am still getting the same error. – Aslı Kök May 12 '20 at 16:56
  • If the error message doesn't match the code you think you are running, then it still seems like maybe you haven't saved files/checked in changes/pushed or pulled changes/restarted the server. If you show the full traceback then it might give a clue what is going on. But if the error message doesn't match the code you have posted, then it's really difficult to help. Hope you figure out the problem. – Alasdair May 13 '20 at 11:10

0 Answers0