Django custom 404 page not recognized - django

I've placed a custom 404.html page in my root templates directory, but whenever an invalid URL is requested, the default error page is given. Strangely in production, "Internal Server Error" is displayed, while "Not Found.
The requested resource was not found on this server." is displayed on localhost.
Debug is set to false in both cases.
app/templates/app/404.html:
{% extends "app/base.html" %}
{% block page_header %}
<h1>404</h1>
<h2>The page you requested is not available.</h2>
<i class="far fa-meh"></i>
{% endblock page_header %}

I had to create a new templates folder in the root (not in an app folder) without any sub-folders. The file is located now in root/templates/404.html

Related

VSCode: Auto Format on Save

I use last version of VSCode, I am on Windows10.
When I do the following:
{% extends 'base.html' %}
{% block content %}
<h1>Test About section</h1>
{% endblock %}
The code is automatically auto formatted when I save:
{% extends 'base.html' %} {% block content %}
<h1>Test About section</h1>
{% endblock %}
I am trying to deactivate it, but I have been unable to. This is what I've tried:
CTRL + Shift + P: 'Disable all Extensions' and also 'Disable all Extensions in Workspace'
In the Settings, the "editor.formatOnSave" is set to false (I have checked in User Settings, Settings for the Workpspace, Settings for Workspace JSON, etc)
Disabled Jinja and Prettier
Even tho I disabled my Extensions, when I hit [Save], the code is automatically formatted.
I am not sure where the settings get imported.
The project is new, I use Django, it's not linked to Git as well.
What I am doing wrong? I've been reading articles for the past hour but the issue keeps occurring, did I miss a setting or does a hidden setting get imported somewhere?

How to replace localhost in emails with correct domain for django reset password?

I am serving a django app with a reverse proxy via nginx. The app runs locally under localhost:8686 which is mapped to example.com. So the correct link to reset a password would be something like:
https://example.com:8686/accounts/reset/MQ/591-05377c39db92f2d5368a/
but this is what appears in the emails:
https://localhost:8686/accounts/reset/MQ/591-05377c39db92f2d5368a/
Obviously, django (locally served with waitress) does not know about the domain name. Is it possible to tell django which domain name it should be using?
First way to do what you want is by overriding the default template by creating a custom registration/password_reset_email.html:
{# yourProject/yourApp/templates/registration/password_reset_email.html #}
{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://domain.com{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
Another way to do this is by enabling the "site framework" (see the link #2).
Docs and sources:
https://docs.djangoproject.com/en/2.2/topics/auth/default/#django.contrib.auth.views.PasswordResetView
https://docs.djangoproject.com/en/2.2/ref/contrib/sites/
https://github.com/django/django/blob/master/django/contrib/admin/templates/registration/password_reset_email.html
https://github.com/django/django/blob/1e429df748867097451bf0b45d1080ae6828d921/django/contrib/auth/forms.py#L277

problem with images on a custom 404 page, django

I am trying to make custom 404 page by adding an image to the page. Problem is that image is not displaying on a page. This image is accessible from any other page in each app in DEBUG TRUE or FALSE mode, no difference, except 404 page where it represented as a just an empty frame.
Image is placed in app/static/app root. Tried to place it is main static root -no difference.
# urls.py
handler403 = curry(permission_denied, exception=Exception('Permission Denied'),
template_name='errors/403.html')
handler404 = curry(page_not_found, exception=Exception('Page not Found'),
template_name='errors/404.html')
handler 403 and handler 404 works fine, except images failure.
# template
<!-- templates/errors/404.html -->
{% extends "base.html" %}
{% block title %} error 404 {% endblock %}
{% load static %}
{% block content %}
<img src="{% static "appname/404.png" %}" alt="404">
{% endblock content %}
Problem is only on 404/403 pages. Everywhere else -no problems at all. Text itself displays normally.
Question is what should I check in terms of possible pitfalls I possible keeping out of scope ?
thanks in advance
p.s. tried
<img src="{% get_static_prefix %}404.png" >
still no effect
Solution is to run server in the following mode:
manage.py runserver --insecure

Using an embedded Iframe in Django?

How do I use an Iframe in Django. Currently its searching for the src url in my own files. How do I tell it to not look in my files but to look at the url?
Currently I'm storing the embed code minus the tag in a database and then trying to dynamically generate it in my template.
games.embed = 'allowtransparency="true" width="485" height="402" src="//scratch.mit.edu/projects/embed/82384372/?autostart=false" frameborder="0" allowfullscreen'
{% extends 'code_games/base.html' %}
{% block content %}
<div class="game">
{{games.title|linebreaks}}
<iframe '{{ games.embed }}'></iframe>
</div>
{% endblock %}
The iframe itself shows up on my page but the contents of it don't.
The request URL per the error:
Request URL: http://chacemcguyer.pythonanywhere.com/games/1/%22/scratch.mit.edu/projects/embed/82384372/?autostart=false%22
You can see that its searching for the url in my site. How do I get around that?
The error also says:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
Then it shows all of my urls from settings.py
I found a solution!
What I had to do was make sure that the iframe content which was coming from my database was |safe
I changed:
<iframe src='{{ games.embed }}'></iframe>
to
<iframe src='{{ games.embed|safe }}'></iframe>

Edit Django admin logout template?

I want to make a very small change to the Django admin logout page.
I know how to use templates to override the Django admin templates, so I have tried to do the same thing with the logout file.
I have set up a new template at templates/registration/logged_out.html. The content of this file is as follows:
{% extends "registration/logged_out.html" %}
{% block content %}
<p>Thanks for using the site.</p>
<p>Log in again</p>
<p>Return to the home page</p>
{% endblock %}
However, something is definitely wrong, because when I try to log out of admin, the site stops running.
I've found the Django docs page recommending the use of AdminSite for changes to the base template and logout pages, but is this really necessary for such a tiny change?
If so, does anyone have an example of how I might set up the logout template? I'm rather intimidated by the instructions for AdminSite.
Thanks.
The reason of manage.py runserver termination is an inheritance loop.
Django loads "registration/logged_out.html" and that it tries to load it's parent: "registration/logged_out.html". Unfortunately parent is the same template and so we end up on the template inheritance loop.
Manage.py will terminate with some variant of stack overflow error...
You can easily escape the issue by extending the parent of original "registration/logged_out.html" -> "admin/base_site.html". I.e:
{% extends "admin/base_site.html" %}
{% load i18n %}
{% block breadcrumbs %}<div class="breadcrumbs">{% trans 'Home' %}</div>{% endblock %}
{% block content %}
<p>Thanks for using the site.</p>
<p>Log in again</p>
<p>Return to the home page</p>
{% endblock %}
You're getting a template import loop. The template loader won't load the base template form wherever you've got Django installed, because it sees that you have that template in your project's template folder.
I think you'll need to copy the log out template from where you have Django installed to your project's template folder. Unfortunately that's the only way that seems to work. This method also means that if updates are made to the Django admin templates, you'll have to manually apply them to your modified templates.