Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (19.9k points)

I have searched the similar questions but did not find my answer.

My simple webapp structure is:

mysite/

    manage.py

    mysite/

        __init__.py

        settings.py

        urls.py

        wsgi.py

    static/

        test.txt

    myfirstapp/

I have set STATIC_URL = '/static/' and STATIC_ROOT = os.path.join(BASE_DIR, "static")

and when I start the django server, I print the static path out which is /home/pi/mysite/static, looks correct

In my template, the code is:

{% load static %}

<li><a href="{% static 'test.txt' %}">Test File</a></li>

If I click the link, I always got 404. However, if I put the static folder in /home/pi/mysite/mysite/, then the static dir is /home/pi/mysite/mysite/static, this code can work and I can view the txt file.

I am confused by the settings, and I thought I set the static directory in settings in the root directory but not in the "mysite/mysite". Why the real behavior is opposite? What is the problem here? Thanks!

1 Answer

0 votes
by (25.1k points)

You should serve them during development. Check this part. Edit your main urls.py(you'll find it in same folder as settings.py).

from django.conf import settings

from django.conf.urls.static import static

urlpatterns = [

    # ... the rest of your URLconf goes here ...

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Browse Categories

...