I'm trying to make my views.py point to a HTML page I've made that has embedded CSS, is this the best approach? I'm also running Django locally for testing purposes until it is moved to a production server how would I make local links to point to my HTML ?
https://docs.djangoproject.com/en/1.10/ref/settings/#templates
The default includes app/templates/ to your template paths and i would recommend to store your template files there.
Static files like .css and .js are served in similiar way
https://docs.djangoproject.com/en/1.10/howto/static-files/
The django test server also serves static files like css and js for you.
I would also recommend to move your css in one or more seperate .css files.
It helps you on the long run to keep your project clean.
1) In your views.py should be something like:
def my_view(request):
return render(request, 'my_html_page.html', {})
2) You should put 'my_html_page.html' to folder "templates/"
3) You should make folder "static" and put there you css file (i.e. styles.css)
4) in "my_html_page.html" you should link your css like this:
<link rel="stylesheet" href="***{% static 'styles.css' %}***" rel="stylesheet">
5) you should run command
python manage.py collectstatic
p.s. on production server you should install whitehoise
pip instal whitenoise
Related
I copied a HTML page with many images and javascript codes.
I created the static folder and did everything that is needed, and when I'm tying to load the source with static tag such as:
<link rel="stylesheet" type="text/css" href="{% static 'styles/bootstrap4/bootstrap.min.css' %}">
I'm still getting an error:
GET http://127.0.0.1:8000/static/styles/bootstrap4/bootstrap.min.css net::ERR_ABORTED 404 (Not Found)
All it did is to add static in the url and I dont understand why?
I cant find how to load jinja in pycharm so my tags are white.
Have you looked at the Django Documentation?
Serving the files
In addition to these configuration steps, you’ll also need to actually serve the static files.
During development, if you use django.contrib.staticfiles, this will be done automatically by runserver when DEBUG is set to True (see django.contrib.staticfiles.views.serve()).
This method is grossly inefficient and probably insecure, so it is unsuitable for production.
See Deploying static files for proper strategies to serve static files in production environments.
I have an Angular app which I'm trying to serve from Flask server. The app runs on Flask but when I click a link on the app, the URL becomes ...../static/# instead of ...../# which results in PageNotFound error.
Basically, /static is being added to path. Do the files have to be placed in a specific way to avoid this issue? I have tried changing templateURL but don't quite know how this works.
This is what I've done: created template, static inside application. Copy-Pasted my Angular UI inside app. Created .py file to run the app. Built the angular app using ng build --base-href /static/. Then copied the .js files to static and index.html to templates folder.
The first page is alright but on clicking a link i go to ..../static/# which results in PageNotFound error.
IN A NUTSHELL, STATIC FOLDER CONTENT IS UNREACHABLE!
Also glyphicon isn't rendering properly when running on Flask but renders fine when running otherwise on localhost:4200. Instead it shows some other symbol on Flask. Some other styling also goes out of the window while using Flask but works otherwise.
Any suggestions are welcome.
Got it to work.
Just changed <a href="#"> to <a> and it started working!
Try building it using ng build --base-href or just ng build.
If this doesn't work then please show me your angular route and index.ts files.
In my projects, i never server static files from Angula/React/Vue from flask using CORS, i normaly create a separete HTTP server for frontend, it is egual how the server will be with Docker and Kubbernets,but if you prefer in this way, you can do something like this
from flask import send_from_directory,
#app.route('/<path:path>')
def send_static(path):
return send_from_directory(app.static_folder, path)
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.
Now I have a django project and some small html project (in fact, some html5 games). How can I added the html site to the django project?
For the each html project has ref a lot of css, img and javascript in its own folder, and different html site are in different folder.
Hence, I can not now just do like this:
t = get_template('htmlprojectfolder/index.html')
html = t.render(Context())
return HttpResponse(html)
I think my problem is:
"how can I jump to a html index page with its static files correct linked?" (Clealy, I can not read just the index page as a template file.)
Thank you for help!
I think the best way to add static, non-Django files that shall be accessible via a Django project would be to add the files to the STATICFILES_DIR. You can read more about static files here in the Django documentation.
Another, and possibly better, way to handle this would be to let the webserver serve the static files separately from Django.
newb to pyramid and python. I've been able to link to my static files successfully in any of my jinja2 templates, ie:
<link rel="stylesheet" type="text/css" href="{{'myproject:static/mycss.css'|static_url}}"></link>
The .css file loads fine and I can link to any images that are inside my static folder as long as I do it within the jinja template.
I'd like to use an image as a background but am having trouble linking to the image in my css file:
#mydiv{
background-image:url("{{'myproject:static/myimage.gif'|static_url}}");
}
This link shows up in mycss.css as
"{{'myproject:static/myimage.gif'|static_url}}"
and doesn't show up as a link. (if I load an externally hosted image as my background-image it works)
thx!
Your CSS file is a static file, and thus is not treated as a template. All static resources are served as-is, without any processing.
All non-absolute URLs in a CSS file are relative to the location from where the CSS file has been loaded; if you use background-image:url("myimage.gif"), the browser loads the image relative to your CSS file location. Since the CSS was loaded from http://yoursite/static/mycss.css, the image will be loaded from http://yoursite/static/myimage.gif.
Alternatively, if you referencing files from unusual locations (for example, images auto-generated by one of your views), you'll have to add your CSS file as a view instead, and render it with a (text) template.