Sporadic wrong urls - django

We have an application running built on python/django. I get emails whenever there is a missing link (404) or a server error (500). I've noticed that i get a number of links that looks something like this:
Referrer: http://www.mywebsite.com/model/
Requested URL: /model/js/modernizr-1.7.min.cb14102011.js
User agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB7.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
IP address: 41.2.246.84
The issue is the requested url. It requests the js file relative to the current dir, rather than the absolute dir. What makes it bizarre is that I can't reproduce it, and it happens a handful of times a day, even though there are many different users on the site. The consequence of the missing links also sometimes causes 500 errors, depending on the url (due to the django url structure I've defined for the application).
Has anybody seen this kind of issue before? Is there anything that I should check for that might resolve this issue?
Edit:
The js files are accessed through http://www.mywebsite.com/static/js/ The same configuration applies for css files. On further investigation it seems that the 'static' part gets dropped from the url.
Edit (2)
I made a mistake yesterday. not only does the {{ STATIC_URL }} not pull through as defined in the settings file, it is replaced with the current (relative) url. I.e, in the original post "model" should be "static", but its not replaced as such. I'm still at a loss as to why this happens.

Thanks Dennis and Jordan. Your comments helped me a lot.
For my scenario I noticed the problems only happens on 1 url. Since many of the templates use the same base template. I ruled out a base template problem. I also checked the views of the url to see that the context was added correctly (since without context the STATIC_URL variable might not pull through). All good.
I also only saw IE7 in the logs. When I looked at the template I noticed an extra closing " (ie class="bogus"" ). I think that extra quote messed up the template, and IE7 then had trouble rendering the rest of the template.
I've made the changes in production so am not 100% sure that I've fixed the problem, but I'll know within a day or so.
Edit:
The answer, I've realised, lies in the fact that if there is a coding problem (ie the server throws a 500), then the context doesn't get loaded correctly, and so the STATIC_URL doesn't get populated. This also means that each 500 error will have a corresponding 404 error for any files loaded from STATIC_URL

Related

javascript file loading problem in a web browser, relating to mime types, context Django debug_toolbar concerning Django/debug_toolbar versions >=3.X

I have noticed, at some point last year, that the debug_toolbar is not present anymore. Now I have found the problem, but do not actually completely understand what is happening and why. [Update: I have found an answer - see the last few paragraphs ]
The initial information is that I tried a lot of different version combinations for Django/delug_toolbar and there were no problems as the version number of both started by 2.
However, the toolbar is not showing itself if a version number starts by 3. The toolbar 2.2 should work with Django version 3 or larger also.
Now I have located the problem to be that toolbar.js is not loaded. There is a message in the developer tool's console:
:8001/static/debug_toolbar/js/toolbar.js:1 Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.
So I found the location of the toolbar.js under the static files folder of the debug_toolbar.
The file is loaded in a template base.html (in folder env\Lib\site-packages\debug_toolbar\templates\debug_toolbar)
There is a line:
<script type="module" src="{% static 'debug_toolbar/js/toolbar.js' %}" async></script>
But I do not understand why type="module" is preventing the browser to load and use the toolbar.js?
There was a section in the (https://readthedocs.org/projects/django-debug-toolbar/downloads/pdf/stable/) Django Debug Toolbar manual that said in case of the problem with mimetypes the user should edit Windows registry.
"1.6 Troubleshooting
On some platforms, the Django runserver command may use incorrect content types for static assets. To guess content types, Django relies on the mimetypes module from the Python standard library, which itself relies on the underlying platform’s map files. If you find improper content types for certain files, it is most likely that the platform’s map files are incorrect or need to be updated. This can be achieved, for example, by installing or updating the mailcap package on a Red Hat distribution, mime-support on a Debian distribution, or by editing the keys under HKEY_CLASSES_ROOT in the Windows registry"
Could someone please give me an explanation for what is going on here? I tried to find if there was any discussion about this problem with these new Django/Django Debug Tool versions but there was nothing to be found. The other discussion was concerning about the earlier versions.
Do I have a security setting of a browser (etc) that is causing the problem as there is no question about the people that have made the Debug toolbar would have implemented their code/settings not to work at all? I have tried Firefox/Edge/Chrome and the issue is in all of them.
I found a tip from another question.
By adding
if DEBUG:
import mimetypes
mimetypes.add_type("application/javascript", ".js", True)
to settings.py causes that the toolbar.js is loaded.
That addition solved the whole problem. There is no need to do anything for the Windows registry.
The discussion can be found here:
Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking i
So my question was in fact a duplicate.

Why is my site not fully rendering after implementing WP Rocket? I get "Uncaught SyntaxError: Invalid regular expression: missing /" in my console

I've recently implemented WP Rocket on my WordPress site and suddenly it's not rendering correctly. I'm getting this error message in the console:
Uncaught SyntaxError: Invalid regular expression: missing /
I can clear the cache and it loads the first time I load it, but then fails to fully render on all subsequent loads. I've disabled all other plugins and I still get the same error. I'm using a popular theme Divi which is not seeing the same issues on other sites I've got with Divi.
I created a blank site with Divi and loaded only WP Rocket with the same settings. Turns out the page fully loads, but I still get the same error in the console:
Uncaught SyntaxError: Invalid regular expression: missing /
I'm fairly certain it's related to the WP Rocket cache at this point. Here's the full WP Rocket cache settings:
[x] Enable caching for mobile devices
[x] Separate cache files for mobile devices
[ ] Enable caching for logged-in WordPress users
Cache Lifespan: 1 Day
I've seen others post about this same issue, but no solutions other than clearing the cache - which only works on the very next load, not subsequent ones.
As I was typing this up and listing out the cache settings, I noticed the WP Rocket section for File Optimization which led me to finding a solution. By unchecking Minify HTML and Minify JavaScript file I was able to fully eliminate the issue.
Further research uncovered that this may the caused by a dependency of WP Rocket called Minify.
Hopefully this will help anybody else who stumbles upon the same issue. My site is running very quickly now with WP Rocket despite not minifying the HTML and JavaScript.
It's about DIVI: When you checked minify in the DIVI builder, it will regenerate the minified files each time you do a new post/page. But not the same filename, DIVI uses each time a different filename for the minified files. So if minify the minified files by another plugin you wil drive into trouble.
Easy solution for every cache-plugin with DIVI: Uncheck minify in the DIVI-Builder and let your cache-plugin do the job

Coldfusion per application mapping randomly fails

In my application.cfc file I have the following code:
this.mappings["/includes"] = "#expandPath('../../priv/inc/')#";
and then on a page I have the following CF include set up:
<cfinclude template="/includes/imageOptimise-thumbnail.cfm">
For the majority of the time when the page is submitted the imageOptimise-thumbnail page runs without any issue but occasionally it will error saying that it can't find the imageOptimise-thumbnail.cfm. If I resubmit the page, chances are it will work but the question is, why is it sometimes not being able to find the page?
I had a look in the CF Admin and I can't see anything in the logs saying that there was an error but it is clearly showing a Coldfusion 'template not found' error message when it fails.
Anyone got any ideas?
I think your issues stem from the fact that the path used for the mapping is relative.
According to advice in ColdFusion Mappings and Relative Paths,
the moral of the story is to NEVER use expandPath() to create a
mapping that’s relative to the webroot.
Try to make the webroot static as the blog suggests
<cfset this.mappings["/myapplication"] =getDirectoryFromPath(getCurrentTemplatePath())
and navigate to /priv/inc/ from the webroot

Django view getting called twice (double GET request)

I'm creating a classifieds website in Django. A single view function handles global listings, city-wise listings, barter-only global listings and barter-only city-wise listings. This view is called ads.
The url patterns are written in the following order (note that each has a unique name although it's tied to the same ads view):
urlpatterns = patterns('',
url(r'^buy_and_sell/$', ads,name='classified_listing'),
url(r'^buy_and_sell/barter/$', ads,name='barter_classified_listing'),
url(r'^buy_and_sell/barter/(?P<city>[\w.#+-]+)/$', ads,name='city_barter_classified_listing'),
url(r'^buy_and_sell/(?P<city>[\w.#+-]+)/$', ads,name='city_classified_listing'),
)
The problem is that when I hit the url named classified_listing in the list above, the function ads gets called twice. I.e. here's what I see in my terminal:
[14/Jul/2017 14:31:08] "GET /buy_and_sell/ HTTP/1.1" 200 53758
[14/Jul/2017 14:31:08] "GET /buy_and_sell/None/ HTTP/1.1" 200 32882
This means double the processing. I thought urls.py returns the first url pattern matched. What am I doing wrong and what's the best way to fix this? All other calls work as expected btw (i.e. only once).
Note: Ask for more information in case I've missed something.
Great explanation to understand these type of occurences: https://groups.google.com/d/msg/django-users/CRMMYWix_60/KEIkguUcqxYJ
This issue has nothing to do with how url patterns are ordered in urls.py.
Like pointed out in the comments under the question, this has to do with problematic asset references in the HTML template.
What does that mean?
For instance, try curl -i http://localhost:8000/example/ >> output.txt in your terminal. Then open up output.txt in your editor of choice. Now search for href or src attributes where values are None (or otherwise malformed). That's one reason a double call is being created. That was the reason for me. I removed these, and the double call disappeared.
There's this old - but relevant - writeup about how to comprehensively diagnose this problem on your machine here: https://groups.google.com/forum/#!msg/django-users/CRMMYWix_60/KEIkguUcqxYJ
Happy testing.
As I can't comment on other answers, just to add for future wanderers that for me the "problem" was in a correctly formed but yet for the browser instructing <iframe src="#"..> tag. On django server the view was rendering twice, once with original request and then again by the hidden iframe element that I used for some of the modal popups later in the page usage.
After emptying the src attribute like <iframe src=""..> a second request is no longer initiated and my modals work fine.
The solution actually is from the link posted already in answers before [https://groups.google.com/forum/#!msg/django-users/CRMMYWix_60/KEIkguUcqxYJ][1]
where it is explained:
Note that it's a URI. That means something that is retrieved. Since
you've used the value "#fff", that will be interpreted by the browser as
a reference to the current page (#fff being an anchor, and not passed to
the server). Ergo, a second request is made.
that the iframe src # (anchor) is instructing the browser to load again the same URL, for the iframe element in my case.
I indeed had several style elements with #fff colors inside and whatnot, but this wasn't it, as browsers are smart enough to recognize this is not an anchor.
With available tools (browser only) I found to be easy to debug and find these initiation href/src attributes over the Network tab of your browser developer tools - in Chrome is just by clicking the Initiator link of the corresponding row - giving you the exact line from the page source that initiated the request to the same URL.
I struggled with the same problem and just wanted to share my experience with it. I had double requests all over my application but everything seemed to work as expected apart form it.
What Daniel Rossman pointet out in the comments was actually also true for my problem. I had a <link rel="shortcut icon" href="#"> in my base template which caused the double request, because of the #, which is a reference to the page itself. Once i removed it, i had no double requests anymore.
Hope this answer can save someone some debugging time.
I got double request in view function, in my scenario, this went wrong:
<img id="profile-img" src="#" alt="" class="profile-cover">
by setting src="" dismiss double request. it was a silly thing, I just thought it apply to a then must apply to img, but img actually send another request.

Dajaxice: Having trouble getting Dajaxice working

I know, I know, the first thing you are thinking is to check all of the instructions online and on dajaxproject.com
Well I have checked and checked and rerun this installation and I cannot figure out what the problem is. I assume it has something to do with the version of Dajaxice I am using and the version the installation instructions are, but those details are usually missing.
At this point in the process, I have everything setup to accomplish a very simple task. Load a webpage from the IP:8000 (ie 1.2.3.4:8000) and have that page load a simple html form button. Once that button is pressed, the page should then send a mesage (using dajaxice) to the server, and once it returns an alert box will pop up, proving that the dajaxice install is working.
So, I have:
Installed Dajaxice (and dajax for that matter), by getting the zip from here: github [dot] com/jorgebastida/django-dajaxice/
Changed everything in a basic Django project to follow the details in this tutorial: http://django-dajaxice.readthedocs.org/en/latest/installation.html
I have setup all of the details of ajax.py, and index.html to work with this example: django-dajaxice [dot] readthedocs [dot] org/en/latest/quickstart.html
PS Sorry for the "[dot]"s, but being a new registered user they limit you to 2 per post, so I tried to give you the best links clickable.
I am stumped. I have been trying many different things, but the documentation available seems so sporadic. It's gotta be something simple I am messing up, but I cannot seem to find it.
Currently, the "Say Hello!" button shows up on the screen, but nothing happens when you press it. Any ideas?
Here is a link to the zip file of the Django project I am working with here: https://docs.google.com/open?id=0B5rzYk6W5HlrYk9veVRfM3JkTGc
The way I have it all organized, DjangoAjax is just the folder containing the Django project. ajax_prac is the project, and practice is an app. Also you'll see a folder called django_media...that's pretty self explanatory, it just holds all the "media" for my Django projects, including jquery.js and jquery.dajax.core.js
If there are some details that I have smudged in the code, contrary to the installation guide, I apologize. I have been working through so many iterations of this, that it is likely I could've forgotten to change something back when making this post.
Can anyone help?
All the documentation weirdness is probably related with the new release I made yesterday (8 hours ago). Probably you was using an old version of dajaxice reading the new documentation.
I've release django-dajaxice (0.5) and django-dajax (0.9.1), both fully compatible with the last version of Django (1.4.1).
django-dajaxice: http://pypi.python.org/pypi/django-dajaxice
django-dajax: http://pypi.python.org/pypi/django-dajax
For this release I've also rewrite both documentations and hosted them on readthedocs. Also, http://dajaxproject.com code is now opensource so everybody can read the code and see how dajaxice works in production.
django-dajaxice: http://django-dajaxice.readthedocs.org/en/latest/
django-dajax: http://django-dajax.readthedocs.org/en/latest/
example project: https://github.com/jorgebastida/dajaxproject.com
Could you please upgrade to django-dajaxice (0.5) follow the new installation steps (quite similar http://django-dajaxice.readthedocs.org/en/latest/installation.html) and post here any traceback/issue you could have?
Thanks
In your example code ( DjangoAjax / ajax_prac / templatedirectory / practice / index.html):
<input type="button" value="Send message to server!"
onclick="Dajaxice.ajax.sayhello(my_js_callback);">
Should be:
<input type="button" value="Send message to server!"
onclick="Dajaxice.ajax_prac.practice.views.sayhello(my_js_callback);">
I.e. It must reference a Python module that contains your registered Dajaxice view. And according to your sample code, ajax_prac.practice.views exists while ajax.sayhello does not.
Also the Python module must be prefaced with "Dajaxice" (which is probably a significant source of confusion).