Getting 302 and 404 error with Javascript - javascript

I am trying to run WMDEditor in my Django site. I have installed WMD files in a directory called /static/js/wmd.wmd.js of the root of my website.
However when the page get served I get:
INFO 2009-09-08 11:00:48,217 dev_appserver.py:3034] "GET /static/js/wmd/wmd.
js HTTP/1.1" 302 -
INFO 2009-09-08 11:00:48,733 dev_appserver.py:3034] "GET /static/js/wmd/wmd.
js/ HTTP/1.1" 404 -

I had some similar problems. Make sure you also add to your app.yaml file
handlers:
- url: /static
static_dir: static

What is happening is a redirect, because Django thinks your URLs should end with a slash (/), but even with a slash at the end, the url is obviously incorrect.
Add this to your root urls.py file and make sure that settings.MEDIA_ROOT is an absolute path to your static directory:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
Also note that in a production setting static files should not be served by Django.

The django dev server serves admin static files automagically.
However, I don't know how it behaves with static files you yourself add. I've gotten the WMDEditor working in a production enviroment, but not a dev one.
This article outlines how to get a hackish way to work in the dev enviroment to serve static files. You'll want to set up an alias in the production enviroment however.

Related

Images not showing up on server but working fine on localhost in node js

I'm working on a node js app and I have some images which I want to show on the frontend but after publishing the app on the digital-ocean server images are not showing up
Below is how the image directory structured
|__ static
|__ assets
|__ favicon
|__ images
Index.js
app.use(express.static(__dirname + '/static'));
HTML files (Using EJS)
<img src="/assets/images/banner.png">
Images are working fine on localhost but not on the server. Can't figure out the reason for this, Any leads would be great.
Update:
I checked for folder permission and it seems fine.
Found a work around but not sure about the reason for this issue, I removed the parent directory assets and moved the child directory directly under static and after this it worked perfectly fine.
Any explanation on this issue might help to understand.
Have you checked the folder permission ? That might be blocking you from publicly accessing files within the folder. You want to have 755 permission.
I had this issue, and it was because I had a nginx configuration causing issues. When I looked in the server logs, I saw the server trying to serve files located /usr/share/nginx/html/ instead of where they were actually located. After I deleted it, the issue went away.
File: static.conf
location ~* \.(?:jpg|jpeg|gif|png|ico)$ {
expires 7d;
access_log off;
add_header Cache-Control "public";
}

URLs don't work within production - return 404

This issue seems to be well documented but all solutions people suggest on posts don't work for me :/
I managed to fix urls giving 404 on my localhost by applying
--history-api-fallback
to the "scripts" package.json
however when I move the production over to my apache2 setup, any url I manually type or refresh returns 404, on site navigation works fine.
for example, http://www.craftmc.net/ works but http://www.craftmc.net/login returns 404, despite that url working if i navigate to it on site.
I saw a post saying use this .htaccess,
https://hastebin.com/alifavacil.apache
Sadly it didn't work. :/
navigation in question - https://hastebin.com/xevecefoda.xml
You need to configure Apache to serve your app on all URLs that it's supposed to handle.
By default, Apache will only serve your app when the user requests the specific path where the app is located, say for example www.example.com
If you now try to visit www.example.com/user/123, Apache will try to find a file named "123.html" inside the "user" folder. You have to tell Apache that what it should do is reply with your app even if the path requested by the browser doesn't actually point to your app within the filesystem on the server.
You can do this in several ways, but a common one is to use mod_rewrite. For example, you might try something along these lines:
DocumentRoot "/var/www/example.com"
AliasMatch "^/myapp" "/opt/myapp-1.2.3"
<Directory "/opt/myapp-1.2.3">
RewriteEngine On
RewriteBase "/myapp/"
RewriteRule "^index\.html$" "welcome.html"
</Directory>
For more information about mod_rewrite and the various directives it uses, refer to the documentation, and/or google.

Get web directories with "403 Forbidden" block

I'm trying to view a web directory. It only works when I enter "folder/images/cat.png", if I enter "folder/images/" it gives me:
403 Forbidden nginx/1.11.8
How can I download or see all the files within the folder directory without having to go directly to the image included at the end of the directory website link? (Such as "folder/images/cat.png")
Add this to your nginx config file:
location /path-to-folder {
autoindex on;
}
Make sure there's no index file in the folder you want to view already e.g. index.html/php.
You'll need to have HttpAutoindexModule enabled.
You can also do this on Apache web servers by putting an .htaccess file in the directory you want to view:
Options +Indexes

Run Meteor from within a folder

I've run meteor build to create my bundle, uploaded to the server, it runs fine, however the .css and .js paths are wrong, as it's using the root url. I need to run this from within a /project folder. Again it's running, but 404 on the files as they're not prefixed with /project.
eg. http://domain.com/65d054cb90ff094804072528d222178ddbf625e22.js?meteor_js_resource=true 404 (Not Found)
needs to be http://domain.com/project/65d054cb90ff094804072528d222178ddbf625e22.js?meteor_js_resource=true
I've tried using ROOT_URL=http://domain.com/project node main.js, that gives an unknown path error, i've also tried using Meteor.absoluteUrl('project', {}); in conjuntion with rooturl but again, no avail.
Any of you fine people have any ideas? :) Thanks!
PS. It's running on an apache server with ProxyPass, if that's of relevance.
You could instruct your apache to redirect those calls with a ProxyPassMatch, such as:
<LocationMatch ^/(.*)meteor_js_resource=true$>
ProxyPassMatch http://localhost/project/$1meteor_js_resource=true
</LocationMatch>

How do you use images and assets with HTML while using a Flask backend?

I am trying to make a TicTacToe web app with an AI playing as player 2. To do this I coded the game and AI in python, wrote html/javascript front end, and used a flask server. In the html I have img tags and assets such as (these are embedded in the html file in the traditional manner):
<img id="pic" src="assets/images/hil.jpg" class="X">
or
<audio src="assets/audio/smw_coin.wav" id="marioStart" preload="auto"></audio>
However, I get errors of this form.
127.0.0.1 - - [11/Feb/2016 23:52:22] "GET /assets/audio/smb_pipe.wav HTTP/1.1" 404 -
127.0.0.1 - - [11/Feb/2016 23:52:22] "GET /assets/js/jquery-1.11.3.js HTTP/1.1" 404 -
Flask seems to interpret this as a domain name instead of check the assets folder. Does anyone know how to get Flask to interpret the src request as a directory in the flask templates folder?
PS. This is also an issue because it stops me from including JQuery in my html file.
You want to use url_for.
<img src="{{ url_for('static', filename='images/hil.jpg') }}">
By default, you'll end up with a URL that looks like /static/images/hil.jpg that is served up from your static folder. If you'd like to keep the URLs you have, you have to tell Flask to use a different URL path for static assets.
app = Flask(__name__, static_url_path='/assets')
This will give you a URL that looks like /assets/images/hil.jpg' that is served up from yourstatic` folder. If you've already named the folder assets and you don't want to rename it, you can tell Flask to look for static assets in a different folder, too.
app = Flask(__name__, static_folder='assets')
This will give you a URL that looks like /assets/images/hil.jpg that is served up from your assets folder. You don't need to specify both arguments in this case because static_url_path uses static_folder by default.

Categories