Search code examples
djangodjango-templatesdjango-staticfiles

Static file doesn't exist and html-file doesn't work properly


output:

'static' in the STATICFILES_DIRS setting does not exist.

I've tried os.path.join(BASE_DIR, '/static'), but it is doesn't work. I didn't have this problem before adding new html-file and I didn't change static-folder. Setting.py

STATIC_URL = '/static/'


STATICFILES_DIRS = [
    BASE_DIR / 'static'
]

In static-folder I have placed folder 'main' and css-file. base.html

I also have a problem with the file. It doesn't work correctly. Guess I didn't wire it right.

<a href="{% url 'make_vision' %}"><li><button class="btn btn-info"><i class="fas fa-plus-circle"></i>Добавить запись</button></li></a>

make_vision.html

{% extends 'main/base.html' %}
{% block title %}Добавление записей{% endblock %}

{% block content %}
    <div class="features">
        <h1>Форма по добавлению статьи</h1>
        <form method="post">
            <input type="text" placeholder="Название статьи" class="form-control"><br>
            <input type="text" placeholder="Анонс статьи" class="form-control"><br>
            <textarea class="form-control"><br>
            <input type="date" class="form-control"><br>
            <button class="btn btn-success" type="submit">Добавить статью</button>
        </form>
    </div>
{% endblock %}

urls.py

urlpatterns = [
    path('', views.news_home, name='news_home'),
    path('make_vision', views.make_vision, name="make_vision"),
]

views.py

def news_home(request):
    news = Articles.objects.order_by('-data')[:2]
    return render(request, 'news/news_home.html', {'news': news})

def make_vision(request):
    return render(request, 'news/make_vision.html')

When I started the server, I got an error that this path does not exist.


Solution

  • Do this: settings.py:

    STATICFILES_DIRS = [
            os.path.join(BASE_DIR, "static"),
        ]
    

    urls.py:

    urlpatterns = [
        path('', views.news_home, name='news_home'),
        path('make_vision', views.make_vision, name="make_vision"),
    ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    In templates: make_vision.html:

    {% extends 'main/base.html' %}
    {% load static %}
    {% block title %}Добавление записей{% endblock %}
    
    {% block content %}
        <div class="features">
            <h1>Форма по добавлению статьи</h1>
            <form method="post">
                <input type="text" placeholder="Название статьи" class="form-control"><br>
                <input type="text" placeholder="Анонс статьи" class="form-control"><br>
                <textarea class="form-control"><br>
                <input type="date" class="form-control"><br>
                <button class="btn btn-success" type="submit">Добавить статью</button>
            </form>
        </div>
    {% endblock %}