Not being able to upload image to django project - python

While I have the images stored into my 'img' file it isn't showing up inside of my virtual environment, I will provide a screenshot of both the images inside of my file plus the virtual environment folder structure. Below is the error message.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/projects/img/todo.png
'projects\img\todo.png' could not be found
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

It would've been much better if you included your urls.py and setting.py but I'm guessing you have not set the static root, in any case by reading this django doc page you should be able to figure it out.

Related

Flask can't find local static files even though they exist and the specified path is correct

I use render_template in conjunction with Flask to render an html file, index.html, which in turn references a CSS stylesheet, "templatemo-style.css". Within that css file, references are made to other local files like images and so on.
The folder structure looks like this:
/PARENT FOLDER
/templates
/index.html
/static
/assets
/images
/various files
Within the "assets" folder, there is sub folder called "css" which houses the css file.
The css file makes a call to the following url to grab the website's banner image:
background-image: url(../assets/images/main-banner.jpg);
The problem is, when I RUN the flask application, I get the following 404 error in my terminal:
127.0.0.1 - - [02/Aug/2021 13:13:51] "GET /static/images/main-banner.jpg HTTP/1.1" 404 -
For some reason, the application isn't calling the path that I specified in the code. I've searched all over the code in various different files for the place where this mysterious call is made and I can't find it anywhere. Even if I comment out my "background-image:" line above, I still get the same error. I should note that for the front-end i'm using a css/bootstrap template that I found online and then modified to my liking. I've searched the js code as well and that call isn't made there either.
I'm completely stumped and have no idea where this mystery call is coming from. Anybody have any possible ideas? Thanks
EDIT: figured it out. Its because my browser was caching my css file and therefore not running updated code.
Maybe this would work
background-image: src="{{ url_for('static', filename='filename') }}"

Django admin site downloading style-sheets but not displayed as styled

I have set up a basic Django system and am testing out the admin app. The admin screen displays with raw styles and not the pretty ones shown in the tutorials and specified in the style-sheets.
Following several other questions, I found this is a common problem caused because Django doesn't normally serve static files. However, it does if DEBUG is switched on and I do have debug switched on. I followed all of the suggestions in the answers anyway to collect static files, etc.
If I enter the static file URLs directly they get downloaded and I can see the files in the developer mode of the browser (both Chrome and Edge). So I think the static files are being served.
Furthermore, if I save the page using the browser, it saves all of the static files and then if I open the main page it is shown with the correct styles. So the styles are working.
So it would seem to me that the files are being served and the browser is getting them and it can render them (from a saved file) but it somehow isn't working when served from Django.
Can anyone tell me what is going on?
EDIT:
Here is a strange thing: if, using the Chrome developer tool, I select the base.css file, click somewhere in the text of the CSS (say at the end of a line) and then add a space, suddenly the page appears correctly.
If I then do refresh the page it goes back to unstyled again.
EDIT 2:
I saw a recommendation to install the Whitenoise app to serve static file so I went ahead and did it. I turned off debug and presto! the styles appear. Turning on debug (so I presume django serves the files) and the styles go away. I saved both sites to the file system and compared the directories using a compare tool. There was no difference.
I'm not calling this an answer as the question is:
Why?
Also, I can't have debug on and get styles.

Django doesn't show the CSS of my template

The problem is described in title. I have this template for a blog that I'm creating using Django.
When I open it normally, using double click over the HTML file, it looks like this:
But when I open it via url from the Django project, it looks like this
It only shows a green square (part of the css) but obviously can't open the css correctly.
Any idea to solve this?
In Django you don't open the HTML with double click on the file, you need to run the server first and open your site using the localhost (like you did in the second picture).
Judging by those images, are you sure you put the image in the static folder? In Django, the HTML files stays in the "templates" folder of your app and the css, javascript and images in the "static" folder.
If this answer doesn't help you, then you should post your code here, otherwise I can't find the problem.

Using ViewerJS with Django

I am trying to use ViewerJS in a Django 1.8.2 application. However, the link I am using will not render the PDF. I have the ViewerJS folder under static/js/ and my PDF under static/pdf/docs/. Here is the link I'm using:
Preview
The error I am receiving is a 404. Django passes back an error message "Directory indexes are not allowed here.".
Request Method: GET
Request URL: http://localhost:8000/static/js/ViewerJS/
Raised by: django.contrib.staticfiles.views.serve
If I go to localhost:8000/static/pdf/docs/agreement.pdf in my browser then the PDF downloads just fine. If I go to localhost:8000/static/js/ViewerJS/pdf.js then the pdf.js file downloads just fine. So I'm not sure why it isn't working.
Django's static server is only for development, and is purposely limited. As the error says, it doesn't deal with directory indexes.
You could probably make this work by making the link go directly to /static/js/ViewerJS/index.html.
I have just the same problem. I am trying to access a file located at static/gutenberg/docs/files/something.pdf in Django
The url I created is
"http://localhost:8000/static/gutenberg/js/ViewerJS/index.html/#/static/gutenberg/docs/files/something.pdf".
Inspite of adding index.html to the complete it wouldn't work.I am not sure why.
The path is right because I am able to download the file when I run "/static/gutenberg/docs/files/something.pdf" on my browser.

Django serving static pages from Sphinx

I have a large group of static html pages that I've generated from sphinx, and I'd like to show them on my Django site. I can connect to at least one page by putting the html/ folder form sphinx in a templates directory inside my app, and changing the urls.py file to include
url(r'^$', TemplateView.as_view(template_name="index.html")
Then it finds the index.html file inside myapp/templates/html. However, none of the internal links work (it'll try to redirect through Django and give me a 404 error). Also, the static files won't load in (Sphinx generates a _static folder, and even though I put that in the myapp directory, and the Chrome network tab tells me it's trying to load the css from myapp/_static, still nothing).
Is there any way to make all the links relative to each other inside this project? Alternatively, can I get Django to just serve up the whole project as static pages?
It looks like FlatPages is almost what I'm looking for, but I have more than just a title and content in these pages.
Firstly, you should not be using Django to serve essentially static files. This is what your webserver is for.
If you are using Apache, you can use the <directory> directive to serve these files alongside your Django routes.

Categories