0

I have followed many of the answers provided here but I still cannot open the file.

I have an text file that I would like to render on my home.html. I rewrote my views.py as suggested:

def homepage_view(request):
    module_dir = os.path.dirname(__file__)
    file_path = os.path.join(module_dir, 'my_text.txt')
    data_file = open(file_path, 'r')
    data = data_file.read()
    context = {'intro' : data}
    return render(request, 'home.html', context)

Here is my home.html. The html includes: from . import forms, from . import views:

<div class="intro">
    {% block intro %}
        {{block.super }}
        {{intro}}
    {% endblock %}
</div>

My app/urls.py is:

from page import views

app_name = 'page'

urlpatterns = [

    path('home/', views.homepage_view, name='homepage_view'),
    path('upload/', views.csvUpload, name='csv_upload'),
    path('zip/', views.zipUser_view, name = 'zipUser_view'),
    path('results/', views.results_view, name='results_view'),
    path('ky_outline/', views.display_ky_image, name = 'ky_image'),

]

My structure is:

myproject/
    __pycache__
    __init__.py
    settings.py
    urls.py
    wsgi.py
        my/app ('page')
            __pycache__
            migrations
            static
                page
                    css
                        style.css
                     images
                     media
                         my_text.txt

static and media settings are:

STATIC_URL = '/static/'


STATICFILES_DIR = [
    os.path.join(BASE_DIR, 'static'),

]

STATIC_ROOT = [],

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = '/media/'

What am I missing or have I looked at this too long?

1
  • Based on your configurations for handling static files, why your static directory is under your app directory? Shouldn't it be beside the settings.py? Commented Dec 16, 2019 at 1:54

1 Answer 1

1

In views, try to put the path manually instead of use os, if it's work then is problem of how you give the path to open()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.