Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Bad 1: You shield off useful error pages.<br> Bad 2: Django isn't a webserver so don't serve static content with Django.</p> <p>If a user requests a page that doesn't exist the common thing to do is return a 404 page not found. The 404 page should inform the visitor what happened and what to do next.</p> <p>Example: <a href="https://stackoverflow.com/doesnotexcist/">https://stackoverflow.com/doesnotexcist/</a></p> <blockquote> <p>Django can also be configured to email errors about broken links (404 “page not found” errors). Django sends emails about 404 errors when: DEBUG is False; Your MIDDLEWARE_CLASSES setting includes django.middleware.common.BrokenLinkEmailsMiddleware.</p> </blockquote> <p><a href="https://docs.djangoproject.com/en/1.6/howto/error-reporting/#errors" rel="nofollow noreferrer">https://docs.djangoproject.com/en/1.6/howto/error-reporting/#errors</a></p> <p>Now you can fix your broken links when needed and if you get users fiddling the url just see it as a feature request. With a catch all redirect you're in the dark.</p> <p><strong>UPDATE: How to raise 404</strong></p> <p>In your views.py you can try to get an object and if it does not exists raise a 404 not found:</p> <pre><code>from django.http import Http404 def my_view(request): try: my_object = MyModel.objects.get(pk=1) except MyModel.DoesNotExist: raise Http404 </code></pre> <p>But cleaner is to use a shortcut function:</p> <pre><code>from django.shortcuts import get_object_or_404 def my_view(request): my_object = get_object_or_404(MyModel, pk=1) </code></pre> <p>There is also a <code>get_list_or_404</code>. More about the returning 404, shortcuts and writing views on: <a href="https://docs.djangoproject.com/en/dev/topics/http/shortcuts/" rel="nofollow noreferrer">https://docs.djangoproject.com/en/dev/topics/http/shortcuts/</a> and <a href="https://docs.djangoproject.com/en/dev/topics/http/views/#django.http.Http404" rel="nofollow noreferrer">https://docs.djangoproject.com/en/dev/topics/http/views/#django.http.Http404</a></p> <p>When you raise Http404 from within a view, Django loads and renders the template 404.html if you created it in your root template directory. So add a template the template. Create a file called 404.html in /project/templates/ :</p> <pre><code>{% extends "base.html" %} {% block title %}HTTP 404 - Page not found{% endblock %} {% block content %} &lt;h1&gt;HTTP404&lt;/h1&gt; &lt;p&gt;Snap! Something went wrong. The page you requested could not be found!&lt;/p&gt; &lt;!-- Present some navigation alternatives --&gt; &lt;/div&gt; {% endblock %} </code></pre> <p>Note that this is the presentation for humans. But the real important bit is the status code of the returned data:</p> <pre><code>allcaps$ curl -I https://stackoverflow.com/doesnotexcist/ HTTP/1.1 404 Not Found Cache-Control: private Content-Length: 18290 Content-Type: text/html; charset=utf-8 X-Frame-Options: SAMEORIGIN Date: Fri, 27 Sep 2013 08:50:43 GMT </code></pre> <p>The <code>HTTP/1.1 404 Not Found</code> tells crawler bots that they shouldn't index the page.</p> <p>Note: If DEBUG is True, Django displays the useful 404 error page with traceback intended for developers. But if DEBUG is False (production), then it displays the template we just created. So make sure that in settings.py <code>DEBUG = False</code> to test the template you just created.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload