I have custom 404 and 500 error handlers setup in django and they work in development, but with Apache they just show the default apache Internal Server Error page, even for 404 errors.
Here is my urls.py:
handler400 = 'views.error400'
handler403 = 'views.error403'
handler404 = 'views.error404'
handler500 = 'views.error500'
I've read of many ways to set up custom error pages in apache, but all of the approaches I tried with django did not work. What is the correct way to set this up for Apache/mod_wsgi and Django?
Do I remove those lines from my urls.py and add some lines to my apache.conf?
What lines should be added to apache.conf as I'm not sure how to access views from the apache configuration file and the normal urls.py routing for Django is not working for the custom error pages since as I suspect Apache is intercepting the requests and providing it's own pages.
I just want to use my templates for my custom error pages (which include inheriting from a base template).
How can I achieve this, I can't find a decent answer anywhere?
Thanks
EDIT:
I tried editting the Apache config to add ErrorDocument 500 /templates/500.html and such but it did not work.
I currently have the error page in my templates directory, they each inherit from a base template. Also, they each have their own view to handle the responses.
But no matter what I do, removing those lines from urls.py, adding stuff to apache config, etc. I can't get anything but the typical Internal Server Error apache page to display on any error, even one's that aren't 500 errors.
What can I do to get this working? Worked fine on the django development server, but as soon as apache was thrown into the mix I can't get it to work using either apache or django, I must be missing something but what?
You can remove those lines from urls.py. Django will look up to your templates to get those.
Just put 404.html and 500.html files into your_app/templates/ directory.
Related
I have a system that has deployed django-hosts, all is working fine. Until now I have been handling 404 and 500 errors for production by utilizing the neat trick of just placing files 500.html and 404.html in the root of the templates folder. This very nicely handles these errors in production (debug=False).
However, now I want to get a little more creative with my error handling messaging and feedback to the user. I have other projects where I have created a custom view for the error handling.
e.g.
def custom_error_500(request, exception=None):
print ('error 500 ') #For debugging to verify view is being processed.
return render(request, "errors/500.html", status=500)
and then set this parameter at the bottom of the urls.py (appname is 'main')
handler500 = 'main.errorviews.custom_error_500'
This all works very nicely in a project that does NOT use django-hosts.
The advantage of creating the custom view is that the context is past to the RequestContext to the template, whereas by default not the case (see reference).
Now, I know with django-hosts here is magic going on in the way urls.py are processed for each host. Could this be the reason why?. I tried placing the hander500 setting in all the possible urls.py that I have in project, but I get the same result, which is the system just looks for the 500.html in the root and if it does not find it displays the standard built in Server Error 500 message.
Has anyone else had the same problem? I can not believe I am the only one trying to display a customer error message using django hosts :-).
Thanks in advance for any assistance!
In my settings.py, I have STATIC_URL='/static/'
I have a view which I use as a fallback for the static url
class MyView(View):
def get(request):
return XXX
which I add it to my urls like so
urlpatterns += [url(r'^/static/', MyView.as_view())]
Next, I want to make sure that the fallback URL is working. When I go to a bad link like localhost/static/garbage it shows me the Django 404 error page but it claims that it was raised by MyView.
What's extremely frustrating is I have two apps with the same static files handling. One of the apps trigger my view, the other does not. I cannot tell what's different that's causing the issue. Both are on whitenoise==4.1.x and django==2.3
Run under wsgi instead of runserver.
For some reason when using runserver, Django has a different static assets routing logic where MyView is bypassed and I get a different 404 page. When I switch to gunicorn everything works fine.
I know there is 404 error handling in django.
But is it better to just put that config in nginx ?
This ST thread has the solution for putting it. -
http://stackoverflow.com/questions/1024199/nginx-customizing-404-page
Is that how everyone handles it when using nginx ?
I have created my own 404.html & 500.html in the sites theme, want to display them.
I didn't know how to configure 404 & 500 errors in django.
Thanks to "namnatulco" who helped me.
Here are the steps:
Create 2 pages 404.html & 500.html
Place them in your modules template folder
In your modules urls.conf, enter these two lines:
handler404 = "myproject.mymodule.views.redirect_page_not_found"
handler500 = "myproject.mymodule.views.redirect_500_error"
In your view, define the functions
def redirect_page_not_found(request):
return render_to_response('logreg/404.html', {}, context_instance=RequestContext(request));
def redirect_500_error(request):
return render_to_response('logreg/500.html', {}, context_instance=RequestContext(request));
Test it by giving some incorrect URL e.g. - www.mydomain.com/aaaaaaaaaaaaaaaa
To test 500 error, inside your view, in your render_to_response, give an incorrect URL.
That's it. You should be set.
You haven't mentioned any reasons why you would want to put these pages in the Nginx server. I would recommend keeping it with the rest of your site, that is, on the Django server. Moving part of your site to the Nginx server is a good idea to solve scalability problem, but complicates your deploy. I certainly hope you aren't seeing a significant fraction of your site's traffic going to your error pages!
I recommend using an in-Django 404/500 handler. You can deliver meaningful alternate nav suggestions in a page style that is consistent with the rest of your site.
Make sure you do not return a page talking about the error but sporting a 200 return status -- human will understand it's an error, but programmatic access will not. I'm avoiding saying "search engines" here, but the truth is that they will probably represent 98%+ of your non-human visitors. See HttpResponse subclasses for details.
I finally got my django install working, however I'm noticing that the typical look and feel of the admin pages are missing, and it's as if there are no styles applied to the structure of the pages. Do I have some kind of setup issue or pathing issue that's preventing the style sheets from being found? Where are they stored? My pages look like they are from 1994.
Sounds like your admin media isn't being served correctly. In your settings.py, there's a variable called ADMIN_MEDIA_PREFIX, which specifies the URL where Django should look for them. The actual media files are in "[path to your Python site-packages]/django/contrib/admin/media". When using manage.py runserver, the files are served "automagically". However, when using Apache/nginx/etc it's your responsibility to make sure that your server makes the files available at that URL (using rewrite rules, symlinks, etc). More info can be found here.
I've solved this issue simply with the alias on apache:
Alias /static/admin/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/
Alias admin/media/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/
You need to provide more info for use to help you properly. However, this is most probably because didn't set up your Web server to serve static file, and therefor, the admin CSS is not loaded.
To solve this, got the the admin and look at the HTML source. You'll css the path to the admind css. Make your web server service this file on this path.
I have a website done with Django, that was previously done with PHP and CodeIgniter. I've moved the website to Webfaction, changed the DNS and all other configurations, but now my email is full of errors like this:
Error (EXTERNAL IP): /index.php/main/leer/7497
I don't know why the Django app is looking for pages from the PHP app, specially since the PHP app was in another host.
Are those URLs from your old site? That's probably a case of people having stale bookmarks, trying to navigate to them, and getting 404s. You might want to consider catching those, and redirecting to the new URL with response code 302.
I can't imagine those errors are caused by Django (except in the sense that the reports are from Django reporting 404s, which it does for free).
I agree with above. Just want to add you should use django.contrib.redirects to move the redirects.
You can read more about it here