#๐Ÿ”’ Django settings.py variable issue

18 messages ยท Page 1 of 1 (latest)

lime pine
#

I declared a 'leaders_portion' variable in settings.py of my project:

...
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

AUTH_USER_MODEL = 'AUTH.User'

leaders_portion = 20

And I wanna use this variable in view function:

from django.shortcuts import render
from django.http import HttpResponse
from django.conf import settings
from django.urls import reverse

from AUTH.models import User
import requests as rq, json


def home(request):
    return HttpResponse('home')

def leaders(request):
    print('leaders_portion' in dir(settings))
    leaders_url = f'{reverse("api")}?key=leaders&portion={settings.leaders_portion}&index=0'
    first_portion = json.loads(rq.get(leaders_url))
    return render(request, 'leaders.html', {'user': request.user, 'users': first_portion, 'leaders_url': leaders_url[:-1]})

It gets an error: "AttributeError: 'Settings' object has no attribute 'leaders_portion'", print('leaders_portion' in dir(settings)) prints False.
Whats wrong?

ebon geyserBOT
#

@lime pine

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

chilly panther
little eagle
#

uppercase it

#

The var name

#

Otherwise you won't be able to access it via django.conf.settings

lime pine
chilly panther
lime pine
#

yes, that helped me

#

thank you

chilly panther
lime pine
#

Aaa yeah

#

Ok

#

!close

ebon geyserBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.