custom handler404 => standard error page - python

When I try to use my own view for error 404, I instead get a standard error page. So, what I've done by now:
# root settings.py
DEBUG = False
ALLOWED_HOSTS = [
'*'
]
# blog urls.py
handler404 = 'views.custom_404'
# blog views.py
def custom_404(request):
return HttpResponse("Hello world, I'm a custom error 404")
And, besides, to try it locally on Django test server, I run it like this:
python manage.py runserver --insecure
But what I get when I go to some page which does not exist is:
Not Found
The requested url blablabla
So, for some reason I do not see my own message Hello world, I'm a custom error 404.

404 handlers are not per app, they can only be set from the main urls.py.

Related

How to customize 404 page in django?

In my django app I want to show a custom not found 404 page. But after reading the documentation and coding I am still getting the default 404 Not found page.
I am working with Django 2.2.8
I also set DEBUG=False
Here is my code:
in setting.py I have defined:
handler404 = 'riesgo.views.views.error_404'
Definition in riesgo/views/views.py
def error_404(request, exception):
return render(request,'404.html')
In your main urls.py file
handler404 = 'mysite.views.my_custom_page_not_found_view'
you can read the docs for a more detailed answer.

Django Admin Look strange

It's a new install of Django 3 and I get this admin look for the panel :
I have no error in the browser console
I have already done
python3 manage.py collectstatic
One clue :
To be able to see this admin panel without apache 500 error, i must comment :
django.contrib.staticfiles
in settings.py, under MIDDLEWARE
if django.contrib.staticfiles is called in MIDDLEWARE, I have this apache error :
TypeError: 'module' object is not callable,
My css and js looks ok...
I can't find how to correct this view
from django.contrib import admin
admin.autodiscover()
admin.site.enable_nav_sidebar = False
Add the above code somewhere in your main urls.py file
This issue has been raising in django 3.1.
It's not an error. It's just a new side bar being added therein.
Add the above code to disable it.

Django Custom error page's giving error

I'm trying to make custom error page general 404 and 500.
I'm not trying to raise just making a default if it happens for a user, but every place I'm trying to search and follows a tutorial and so on I always ends up in getting an internal error on both 500 and 404
I'm running django=1.11.6, and I'm running debug False because I need to see if it work of course.
How can I fix this issue and make it work?
And how can I make it so it also gives the user the error text so they can send it to me?
Views.py (In my app folder)
# Error Handlers
def handler404(request):
return render(request, 'handlers/404.html', status=404)
def handler500(request):
return render(request, 'handlers/500.html', status=500)
Urls.py (in my main config folder)
from django.conf.urls import include, url, handler404, handler500
from django.contrib import admin
handler404 = 'staff.views.handler404'
handler500 = 'staff.views.handler500'
urlpatterns = [
url(r'^', include('staff.urls')),
url(r'^admin/', admin.site.urls),
]
Firstly, you don't need custom handlers if you just want to use your own templates - just put your own 404.html and 500.html files in your root templates directory.
Secondly, rather than getting users to send you the error codes manually, you can configure Django to send you errors by email.

How can I override standard handler404, handler403, handler500 in Django?

I have tried using https://docs.djangoproject.com/en/dev/topics/http/views/ tutorial, but still I am getting the standard 404 html page. I want to switch in to my custom view
handler404 = 'myview.views.custom_page_not_found' ,
I did debug it (using eclipse), then the value of handler404(old value -'django.config.default.views.page_not_found) is changed to the new value I have given ('myview.views.custom_page_not_found'). But it's still showing the older 404 page. And I have changed settings.py DEBUG into False then it shows the custom page. But it got some disadvantages (it won't load static files and all, DEBUG = false is not the right way) so I had to reset to True.
Do I have to make some other modification for implementing this?
I think you can not change the 404 page in DEBUG = True mode without difficulty.
There is a hint in the documentation (https://docs.djangoproject.com/en/dev/topics/http/views/#the-404-page-not-found-view):
If DEBUG is set to True (in your settings module), then your 404 view will never be used, and your URLconf will be displayed instead, with some debug information.
Try adding this to the bottom of your main urls.py:
if settings.DEBUG:
urlpatterns += patterns('',
(r'^404/$', TemplateResponse, {'template': '404.html'}))
Swap out the 404.html to the appropriate template you use, I believe 404.html is the default though. Then with debug=True you can test out your 404 page.
If you want to Test it out with Debug=True then you need this at the bottom of your main urls.py instead:
#Enable static for runserver with debug false
from django.conf import settings
if settings.DEBUG is False: #if DEBUG is True it will be served automatically
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
When running with DEBUG = False, don't forget to collect static:
python manage.py collectstatic
Hope this helps, Cheers!

Django-Twilio sending SMS on button click

OK possible noob question here: While learning Django, I thought it might be cool to explore telephony with Twilio. My immediate goal is to create a page with a button that, when clicked, causes a "Hello World" SMS to be sent to my phone. After sorting that out I have some ideas for cooler stuff.
I've completed several Django tutorials so far, and made a few little apps with simple views. But nothing I've learned has particularly shed any light on how to do something like this. I've also investigated (and installed) the Django-Twilio app and the Twilio Python Helper Library, but the docs for neither of these show how to send "hello world" SMS's.
Can anyone point out a resource that might show how to do this? Or, if it's trivially easy, just post some example code?
Edit in response to Kevin Burke:
Thanks for getting back to me, Kevin.
After modifying my urls.py to include:
urlpatterns = patterns('',
# ...
url(r'^sms/$', 'django_twilio.views.sms', {
'message': 'Hello world',
'to': '+12223334444',
'sender': '+18882223333',
'status_callback': '/sms/completed/',
}, name = 'send_message'),
# ...
)
and pointing my browser at
http://127.0.0.1:8000/sms/
the following error arises:
Exception Type: TwimlException at /sms/
Exception Value: Invalid method parameter, must be 'GET' or 'POST'
Perhaps this is because I have failed to make appropriate modifications to the view. But I don't have a good way of figuring out what I'm doing wrong from the minimal examples in the tutorial.
/Edit
twilio employee here.
The problem here is that the built in views for django_twilio run through a series of validation checks to make sure they're receiving content from twilio.com and only twilio.com. This is a security measure built into django-twilio.
There are two things you can do:
Make sure your settings.DEBUG = True in your Django settings, this will turn off the validation. You can then send a cURL request on your local machine whilst it is running like this in your terminal:
$ curl http://localhost:8000/sms/
This should return some TWiML like so:
<Response><Sms>Hello world</Sms></Response>
When you're running this online and you want to do to test this, set up your twilio number to point to http://mywebsite.com/sms/ and text the number. Ensure that settings.DEBUG = False and you should get back a message.
If you have anymore problems, let me know.
Here's the official docs: django-twilio official docs. More specifically, read this part about sending SMS: Sending sms messages
Here is a simple solution :
django startproject projectname
urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('message_api.urls')),
]
settings.py
TWILIO_ACCOUNT_SID = TWILIO_ACCOUNT_SID
TWILIO_AUTH_TOKEN = TWILIO_AUTH_TOKEN
DJANGO_TWILIO_FORGERY_PROTECTION = False
DJANGO_TWILIO_BLACKLIST_CHECK = True
Start new application
python manage.py startapp appname
Inside the app folder:`
urls.py
from django.conf.urls import url
import django_twilio
from . import views
urlpatterns = [
url(r'^api/$', views.home),
url(r'^send/', views.sms),
]
views.py
from django.shortcuts import render
from twilio.rest import Client
from twilio_api import settings
def home(request):
return render(request, 'index.html', {})
def sms(request):
client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
message = client.messages.create(to='TO NUMBER', from_='YOUR TWILIO NUMBER', body='This message is sent through twilio api using django framework by akshat.')
print(message.sid)
return render(request, 'thankyou.html')
Make a templates directory inside your app folder
index.html
<body>
<button class="btn btn-outline-primary">Send Message</button>
</body>
thankyou.html
<body>
<h1>Success</h1>
</body>
`

Categories

Resources