I have two htmls a.html and b.html. a.html is located in the template folder by default. b.html is located in appname/static/images/b.html, because it's a model calculation result in html format.
In a.html, I am trying to include b.html but it's not working, unless b.html is in the same template folder.
<body>
{% include 'appname/static/images/b.html' %}
</body>
questions:
how to include b.html?
how to include b.html dynamically if it's in different folder, e.g. images/username/b.html where username is different.
Here's a minimum viable example using the HttpResponse method:
from pathlib import Path
from django.conf import settings
from django.http import HttpResponse
def my_view(request):
my_html = settings.BASE_DIR / "appname" / "static" / "images" / "b.html"
with my_html.open() as f:
content = f.read()
return HttpResponse(content)
Related
my files in templates
- main.html
- page1.html
my code
def main():
return render_template('main.html')
html file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
users
</body>
</html>
it doesn't work and gives me Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
You should provide the route(url) to the template in the function main() using a decorator like this:
#app.rout("your/url")
def manin()
return render_template("your_template.html")
Another thing, you should create a folder(directory) called templates, and place all your templates in it so that flask can detect them, otherwise, flask will not detect the templates, unless you tell it to look elsewhere and you do not want to bother yourself with it right now. Just put all your templates in a directory called templates that it in the same level as your app.
try this:
In main.py:
from flask import Flask, render_template
app = Flask(__name__)
app.config['SECRET_KEY'] = "YourFlaskSafetyKeyHere"
#app.route("/")
def Home():
return render_template("page1.html")
if __name__ == "__main__":
app.run(debug=True)
in page1.html:
<h1>Home Page</h1>
There you go! You just didn't satisfied the requirements of Flask
I have used static for all the js/css/images path. However, click on the logo redirects me to http://127.0.0.1:8000/static/index.html without any css/js/images. The same works well all other paths in the page(eg: http://127.0.0.1:8000/static/about.html)
Sharing my urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, "index.html")
Please help
Screenshot here
Websites generally need to serve additional files such as images,
JavaScript, or CSS. In Django, we refer to these files as “static
files”.
NOTE: html files are not included, so don't use {% static %} with html files.
To jump to other pages, you have to use {% url %} tag with the name of the view, for example {% url 'index' %} or simply use a relative path Home, that's good to go.
For css/js files are not loading, you may have the wrong settings, follow the instructions here step by step, and remember to {% load static %} in the top of your template
When I run 'yarn start', my link to manifest.json in my index.html file works fine, but when I run 'python3 manage.py runserver' all I get in the terminal is:
Not Found: /manifest.json
"GET /manifest.json HTTP/1.1" 404 2234
This also happens to all of my static links and imports. I'm pretty new to Django and React, and programming as a whole, so I think that I'm just missing something simple, but I can't figure it out.
I've been trying to use {% load static %}, but the link doesn't work, even if I edit STATIC_URL in settings.py to point towards my manifest.json directory. I also attempted to edit view.py and urls.py, but all I get is syntax errors in the terminal. Other than that I'm clueless.
frontend/public/index.html
<html>
<head>
<title>WebProject</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<link rel="manifest" href="manifest.json"/>
</head>
<body style="background-color: #FAF0E6; font-family: Verdana; font-size: 40px;">
<div id="root"></div>
</body>
</html>
frontend/urls.py
from django.urls import path
from . import views
from django.conf.urls.static import static
urlpatterns = [
path('', views.index),
]
frontend/views.py
from django.shortcuts import render
def index(request):
return render(request, 'frontend/public/index.html')
I expected my browser to load manifest.json properly, along with any other links or imports, but I keep getting a blank page.
Im using React inside of Django, so when I tried to import my index.js the same "Not Found" terminal error popped up. Im assuming that if I solve the manifest.json problem, I'll also solve my other import and link problems.
I had the same issue and found this Can't get rid of missing manifest.json error
Well, in my case it was browser cache related and swapping to incognito mode was enough.
The same happened to me (blank page and unable to load manifest.json + react build's static files) and I solved the issues thanks to this excellent article
Solution -> assuming your react app (build etc.) are in a folder called frontend at the same level as your django project, in your settings.py file you need to make sure your STATICFILES_DIRS variable is set like below (don't forget the trailing coma as it is a tuple).
STATICFILES_DIRS = (
os.path.join(os.path.join(BASE_DIR, 'frontend'), 'build', 'static'),
)
In urls.py:
from django.urls import re_path
CHANGE:
urlpatterns = [
...
path('', TemplateView.as_view(template_name='index.html')]
TO:
urlpatterns = [
...
re_path('.*', TemplateView.as_view(template_name='index.html')]
Had the same error and worked for me.
I'm making Django app in which I need to embed many external HTML files in the template. Each HTML file is stored in its own directory, along with the subdirectory that contains all the images. The file structure:
Abstract1
Pictures
image1.png
image2.png
abstract1.html
I use a custom template tag for embedding (see below). My problem: the HTML files are loaded, but linked resources (e.g. img) are not working properly (i.e. they're not being displayed). HTML files use relative urls, which, mixed with the django template base path produce invalid url, but even if I use hardcoded absolute urls the problem remains. I feel like I'm missing something obvious. Is there some proper (or not proper but working) way to overcome such problem?
template
{% load abstracts_extras %}
<!DOCTYPE html>
<html>
<body style="margin-left:10px">
<h2>{{abstract}}</h2>
<b>Authors:</b><br/>
<ul>
{% for author in authors %}
<li>{{author}}</li>
{% endfor %}
</ul>
<p>
<b>Title: </b>{{abstract.title}}
<p>
<hr>
{% include_external filename|add:'.html' %}
</body>
</html>
abstracts_extras
from django.template import Library
register = Library()
def include_external (url):
url = 'file:///' + url
import urllib2
return urllib2.urlopen (url).read ()
If I am understanding well, your templates load but not statics like img.
It would be a configuration error.
You should check both settings.py for Django and httpd.conf for Apache and check staticfiles are properly configured.
Have you any error shown or just images are not loaded (but no error)?
I am making a project in django. I am able to implement html pages but not able to show images from that page.
<img src= "home/ashish/PycharmProjects/django/webapp/family/static/images/error-img.png" />
it is showing error as
Resource interpreted as Image but transferred with MIME type text/html
when I hit the url,Request URL:http://127.0.0.1:8002/home/ashish/PycharmProjects/django_webapp/family/static/images/error-img.png
While if i make a src to online image it works fine.
setting.py
STATIC_URL = '/static/' # You may find this is already defined as such.
STATICFILES_DIRS = (
STATIC_PATH,)
MEDIA_URL = '/media/'
And all the images are inside app/static/images.
Thanks
You have not defined static file URLconf in your urls.py file. You can define them for development server use, like this:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
...
urlpatterns += staticfiles_urlpatterns()
And then you can view your static files at:
http://127.0.0.1:8002/static/images/error-img.png
See: Django docs on Managing static files