Tell me how to enter the relative paths in the project Django? I want all the paths to be written not rigidly, but relatively, including the database, now I somehow didn’t get very well, I started the project on localhost, but the admin looks like ... nothing! Unformatted, without tables, a single bare HTML and DB is not created in the root of the project, but in the root of MySQL. Tell me how to do it right!

from os import path #SITE_ROOT = os.path.dirname(os.path.realpath(__file__)) DEBUG = True TEMPLATE_DEBUG = DEBUG ADMINS = ( ('admin', 'ow@gmail.com'), ) MANAGERS = ADMINS EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = 'ow@gmail.com' EMAIL_HOST_PASSWORD = 'yourpassword' EMAIL_PORT = 587 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'dev', 'USER': 'root', 'PASSWORD': '', 'HOST': '', 'PORT': '', } } TIME_ZONE = 'Europe/Kiev' LANGUAGE_CODE = 'en-us' SITE_ID = 1 USE_I18N = True USE_L10N = True PROJECT_PATH = path.realpath(path.dirname(__file__)) MEDIA_ROOT = path.join(PROJECT_PATH, 'media') MEDIA_URL = '/media/' STATIC_ROOT = '' STATIC_URL = '/static/' ADMIN_MEDIA_PREFIX = '/admin_media/' STATICFILES_DIRS = ( ) STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', # 'django.contrib.staticfiles.finders.DefaultStorageFinder', ) SECRET_KEY = 'o77777777777777777777777k$' TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', # 'django.template.loaders.eggs.Loader', ) MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', ) ROOT_URLCONF = 'site.urls' TEMPLATE_DIRS = ( #os.path.join(SITE_ROOT, 'templates') ) INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'posts', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', ) LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'mail_admins': { 'level': 'ERROR', 'class': 'django.utils.log.AdminEmailHandler' } }, 'loggers': { 'django.request': { 'handlers': ['mail_admins'], 'level': 'ERROR', 'propagate': True, }, } } try: from local_settings import * except: pass 

    2 answers 2

    This is done in settings.py

     from os import path PROJECT_PATH = path.realpath(path.dirname(__file__)) MEDIA_ROOT = path.join(PROJECT_PATH, 'media') 

    ...

    This is certainly not a relative path, but an absolute path, but it is calculated dynamically, depending on the position of the project.

    • something does not work! I updated my question (added code) and I would like to know what I am doing wrong. - tukan
    • The meaning of the phrase "The database is not created at the root of the project, but at the root of MySQL" remains a mystery for me ... In order for the admin panel to be properly framed when you start it not through manage.py runserver, you need to collect static files . - qnub
    • in the root, this is where the default is, the databases are saved, I have it datadir = / var / lib / mysql and how to create a database in the project folder? And, about collecting static files - can I chew? I have this first day of work in Django ... pliiiz! - tukan
    • one
      Why do I need to store the database files in the project folder, if you use MySQL DBMS, I advise you not to bother about it and leave them where they are. Static files - pictures, js, css, etc. Each application can have its own. In particular, the admin is an application that comes with Django and it has its own static files. The server running through manage.py runserver itself finds these files for the rest you need to collect them with the manage.py collectstatic command - they will be collected into the STATIC_ROOT folder from there you need to move them to one of the folders listed in STATICFILES_DIRS - qnub
    • 2
      It is necessary to create a folder in which static files will be stored, from which they will be taken by a web server (which must be configured to take files from there, of course). It should differ from STATIC_ROOT and add this folder to the entry in STATICFILES_DIRS for example: STATICFILES_DIRS = (path.join (PROJECT_PATH, 'staticfiles')) - qnub

    It seems that the theory of the respondents is rather weak;

     MEDIA_URL = '/media/' STATIC_ROOT = '' STATIC_URL = '/static/' 

    Is the beginning on the slash this relative? Take at least the web site '/ test /' is from the root of the site, but relative to this is 'test'

    An example solution is

     STATIC_URL = '/static/' 

    change to

     STATIC_URL = 'static' 

    ps template at the beginning of the development lay in the media, with static can be a problem, it is originally intended for the server.

    • It seems that the practice of respondents is rather weak ... - qnub