1. In the ubuntu22.04 system, use django-admin startaproject xxxx to create a new django project
2. Configure the path of static files in the project configuration file
STATIC_URL = '/static/'
STATICFILES_DIRS = [ # lists or tuples are fine
os.path.join(BASE_DIR, 'static') # You can also configure multiple static file directories, just add the path
]
3. After configuration, create a new static directory under the project root directory, and create a 1.txt file in this directory
4. Start the project
python manage.py runserver
5. Access the static file static/1.txt
http://127.0.0.1:8000/static/1.txt
#report 404 error
6. The solution is as follows: modify the settings.py configuration file
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]#Notice:
will 'DIRS':[]
Change to: 'DIRS': [os.path.join(BASE_DIR),'templates'],