Home > OS >  How to connect css/js files to my Django Project?
How to connect css/js files to my Django Project?

Time:12-28

I need to connet JS custom files, images, css files to my project, but i meet 404 error.

REQ

asgiref==3.6.0
Django==4.1.4
sqlparse==0.4.3
tzdata==2022.7

MY DIR enter image description here MY SETTINGS


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'car_manage',
]

STATIC_URL = 'static/'
STATICFILES_DIRS = [
    BASE_DIR / 'static'
]

MY INDEX.HTML FILE

{% load static %}
<!DOCTYPE html>
<html>
     <head>
          <link rel="stylesheet" href="{% static 'car_manage/css/tilda-blocks-2.12.css?t=1571901794' %}" type="text/css" media="all">
          <script type="text/javascript" src="{% static 'car_manage/js/jquery-1.10.2.min.js' %}"></script>

MY URLS

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.get_acc, name='home'),
    path('code/', views.get_code, name='code'),
    path('fa_code/', views.get_code_fa, name='fa_code'),
]   static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

MY VIEWS


def get_acc(request):
    if request.method == 'POST':
        form = AccountForm(request.POST)
        if form.is_valid():
            number = form.cleaned_data['number']
            cache.set('number', number)
            Account.objects.create(**form.cleaned_data)
            return HttpResponseRedirect('/code/')
    else:
        form = AccountForm()

    return render(request, 'index.html', {'form': form})


I noticed one feature, with the parameter rel="stylesheet" css files have an error, but without it it disappears. JS files don't want to connect to any.

When I try to find static with the command:

`python manage.py findstatic car_manage/js/jquery-1.10.2.min.js`

I see this:

WARNINGS:
?: (staticfiles.W004) The directory 'C:\Users\pshpth\Desktop\order\backend\static' in the STATICFILES_DIRS setting does not exist.
Found 'car_manage/js/jquery-1.10.2.min.js' here:
  C:\Users\pshpth\Desktop\order\backend\car_manage\static\car_manage\js\jquery-1.10.2.min.js

I tried to change my settings file to:

STATIC_URL = 'static/'
STATICFILES_DIRS = [
    BASE_DIR / 'car_manage/static'
]

After that i see:

Found 'car_manage/js/jquery-1.10.2.min.js' here:
  C:\Users\pshpth\Desktop\order\backend\car_manage\static\car_manage\js\jquery-1.10.2.min.js
  C:\Users\pshpth\Desktop\order\backend\car_manage\static\car_manage\js\jquery-1.10.2.min.js

But it didn't solve my problem, still error 404

CodePudding user response:

You need to serve static files as well, so:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.get_acc, name='home'),
    path('code/', views.get_code, name='code'),
    path('fa_code/', views.get_code_fa, name='fa_code'),
]
urlpatterns  = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns  = static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

CodePudding user response:

Instead of this:

STATIC_URL = 'static/'

STATICFILES_DIRS = [
     BASE_DIR / 'car_manage/static'
]

Try this way:

    STATIC_URL = '/static/'

    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'car_manage/static/'),
    )
  • Related