django: url routing to hardcoded static and html files - django

I have a legacy project with a bunch of html files and static files (css, images, js, videos ...). All links in those html files are relative and hardcoded:
"css/main.css" or "img/my_img.jpg" etc.
I need to run that project using django.
I cannot change all those links to kind of
{% load static %}
<link rel="stylesheet" href="{% static 'css/main.css' %}">
but all recipes I found in internet suggest using exactly that method.
How can I:
1) route hardcoded urls like "css/main.css"
2) route hardcoded urls like "another.html" ?

This can be done with much ease like this<!--lets' say image-->
<img src="{% static 'static/images/image1.png' %}">.Please make sure the file directories match with that in the code.
Please visit https://docs.djangoproject.com/en/1.10/howto/static-files for detailed info

Related

How to locate websocketbridge.js in Django using channels websocket?

I am trying to implement websockets using channels in Django project. I am getting 404 for webscoketbridge.js Below is html template.
{% load staticfiles %}
{% block title %}Delivery{% endblock %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link href='https://fonts.googleapis.com/css?family=Satisfy' rel='stylesheet' type='text/css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="{% static 'channels/js/websocketbridge.js' %}" type="text/javascript"></script>
Also, I tried to have a look in the virtualenv/lib/python3.5/site-packages/channels path, there is no js folder or any file named websocketbridge.js
Has anyone solved this issue?
The javascript bridge was removed in v2.1.4. Here's the commit: https://github.com/django/channels/commit/2a9d764ad03927581aa2bfcadccc3e953949cb98#diff-b582cbb2f8294afa8bbe26c4c360a01d
This bit me, in my book that breaks semantic versioning.
As #tobyspark said, the javascript wrapper has been completely removed in the Django-channels 2. You can read more on how the js WebSocket wrapper was working in channels 1 here.
the simplest workaround to clear that error in your browser is to create a file called websocketbridge.js in the path shown in the error, "static/channels/js/", or you can specify any other path in your HTML src attribute matching the location of the static files and then add the code from here.
But you have to find a better implementation. You can use ReconnectingWebSocket. In the channels 2 release documentation, it is stated there might be other third-party packages for the binding but I don't know any other.

Serving static images on django application?

I am have been having troubles figuring out how to serve static images with my django application. However, I have figured out a sort of "hack" to get it working but it doesn't allow for much flexibility.
In the html files I have tried the following...
<img src="{{ STATIC_URL }}textchange/nextarrow.png" class="foobar" id="foobar">
Above: The pages will load when I use this but the images will not work.
<img src="{% static "textchange/nextarrow.png" %}" class="foobar" id="foobar">
Above: The pages will not load, I get a server error
<img src="thefullurltoS3bucket/nextarrow.png" class="foobar" id="foobar">
Above: The images will load fine
I have been trying all these different formats when going to production with Heroku and S3. The question I have is is the way I'm serving my static images correct? I use the last example in which I make the src the full url to the bucket at s3. Is that fine?
It should be noted that when I use the static tag serving css files works fine. Images is the only problem.
Your first and second solutions ( that you got server error and not loading ) is the correct way of using static files which is:
<img src="{% static "textchange/nextarrow.png" %}" class="foobar" id="foobar">
review your settings.py file and set STATIC_URL and STATIC_DIRS and also STATIC_ROOT, if you got server error maybe you set the STATIC_URL or STATIC_DIRS wrong. you can find plenty of posts about these settings.
Also check your image url in browser ( sth like 127.0.0.1:8000/static/files/etc.jpg )see if you can access the image.
Serving static files in Django can be a pain.
First, make sure you use the static template tag correctly in your template:
{% load static from staticfiles %}
<img src="{% static 'path/to/image.jpg' %}">
In your settings, have your STATIC_URL point to something like '/static/', your STATIC_ROOT point to the directory where you'll actually serve your files post-collectstatic, and your STATIC_DIRS to where you'll place your static files within your project (i.e. 'static' directory).
If you're serving from S3, you'll probably follow a tutorial like this one.

Resource interpreted as Stylesheet but transferred with MIME type application/x-css:

I am trying to load css file in HTML template in python Django and getting this error
Resource interpreted as Stylesheet but transferred with MIME type application/x-css:
I am using Pydev , Eclipse, Django framework.
You need to give STATIC URL AND STATIC_ROOT values in settings file of django project and then create static folder inside your App folder. Place all your css and js files inside this folder.Now add this tag in your template {% load static %} and give location of css like <link rel="stylesheet" type="text/css" href="{% static 'style.css' %}" />
Example
STATIC_URL='/static/'
STATIC_ROOT=os.path.join(BASE_DIR,"static")'
STATICFILES_DIRS=(os.path.join(BASE_DIR,'Get_Report','static'),)

static files in pybb

I have some problems with static file, when i use them in pybb. I work on django 1.3 and python 2.6.5.
My settings file is here
I put my css and images in /static/pybb/css and /static/pybb/images
when i put tag like
<link rel="stylesheet" href="{{ STATIC_URL }}pybb/my.css">
in my templates, it doesn't work. This is the same with image
<img src="{{ STATIC_URL }}pybb/images/myimage.jpg">
To sum it up, my own static files don't appear in my project templates.
any idea? perhaps i forgot something
Why are you putting css in package static files directory?
Also directories in STATICFILES_DIRS should not overlaps. Look at example provided with pybbm or even start project from this example.

Django template not loading javascript and css properly due to urlpatterns

When this one runs everything goes fine:
(r"^newobject$", "views.myobjects.newobject"),
All the CSS + JS files are properly fetched from:
static/css/...
static/js/...
When this one runs:
(r"^mybjects/(([a-z]|[A-Z]|[0-9])+)$","views.myobjects.loadobject"),
All the css and JS files that are being fetched, are run trough the urlpatterns and are returning my defailt page:
(r"", 'views.main.index'),
This makes all my CSS and JS code to actualy be HTML. My guess is that i'm giving some noob mistake. Is there any common reason why this should happen? And how to fix it?
Edit:
Css example:
<link href="static/css/style.css" type="text/css" rel="stylesheet">
JS example:
<script src="static/js/libs/date.js" type="text/javascript"></script>
See the difference:
when you access *some url*/newobject the static/css/style.css refers *some url*/static/css/style.css*
when you access *some url*/newobject/whatever the static/css/style.css refers *some url*/newobject/static/css/style.css*
If your URL will always be floating around in depth, include your javascript and CSS using URLs relative to the server root (start them by /) instead of relative to the current dir.