Reputation: 21
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?
Upvotes: 0
Views: 222
Reputation: 22
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()
Upvotes: 1