Preview custom 500 error page in Wagtail? - django

I've made custom 500 and 404 error pages in Wagtail. I can preview the 404 page by typing in a false url. I'm just wondering how I can preview the 500 page?
The custom page has links to static images that I need to check are working.
My urls.py
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from wagtail.contrib.sitemaps.views import sitemap
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
admin.autodiscover()
urlpatterns = [
url(r'django-admin/', admin.site.urls),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^sitemap\.xml$', sitemap),
url(r'', include('puput.urls')),
url(r'', include(wagtail_urls)),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if 'debug_toolbar' in settings.INSTALLED_APPS:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]

The answer at https://stackoverflow.com/a/24660486/823020 has most of these details. You can make a view that raises a 500 error.
You can add a views.py to any app. In that file (taken directly from the linked answer):
from django.http import HttpResponseServerError
def my_test_500_view(request):
# Return an "Internal Server Error" 500 response code.
return HttpResponseServerError()
Supplement this in your urls.py with:
from django.conf import settings
from django.urls import path
# or for Django 1.x do
# from django.urls import url
from myapp import views
urlpatterns = [
# original content here
]
if settings.DEBUG:
urlpatterns += [
path('test_500/', views.my_test_500_view, name="test_500"),
# or for Django 1.x do
# url(r'^test_500/$', views.my_test_500_view, name="test_500"),
]
If it's not directly related to any Wagtail pages, then a utils Django app can work well for generic shared code.

Just add url with TemplateView to your urls.py:
from django.views.generic import TemplateView
urlpatterns = [
url(r"^admin/", include(wagtailadmin_urls)),
url(r"^documents/", include(wagtaildocs_urls)),
url(r"^500/", TemplateView.as_view(template_name='500.html')),
]

Related

You are seeing this page because DEBUG=True is in your settings file and you have not configured any URLs

the explorer in vscode Getting the error: You are seeing this page because DEBUG=True is in your settings file and you have not configured any URLs.
meetups/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('meetups/', views.index) #domain_name.com/meetups/
]
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('meetups.urls'))
]
views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'templates/meetups/index.html')
You probably didn't add your app (meetups) into settings.INSTALLED_APPS, which can be found in your_project_name/settings.py.
Change meetups/templates/index.html to meetups/index.html also add name in your urlpatterns

FIX ImportError: cannot import name 'patterns'

I am writing documentaton for the API I have already built. I have already installed drfdocs and added it in INSTALLED_APPS then configured the urls yet I get the following logs below. I think the version of rest_framework_docs is importing patterns which I believe its deprecated, new versions of django are no longer using it. How do I fix this?
I have django 1.11 and using python3. I get the following logs when I try to run ther server:
Logs
File "/usr/local/lib/python3.5/dist-packages/rest_framework_docs/urls.py", line 1, in <module>
from django.conf.urls import patterns, include, url
ImportError: cannot import name 'patterns'
Views.py
from django.conf.urls import include, url
from django.contrib import admin
from rest_framework.urlpatterns import format_suffix_patterns
# from organizations.backends import invitation_backend
from rest_framework.authtoken.views import obtain_auth_token
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
url(r'api/token/', obtain_auth_token, name='api-token'),
# url(r'^account/', include('accounts.urls', namespace='account')),
url(r'^hr/', include('hr.urls', namespace='hr')),
url(r'^acc/', include('Accounting.urls', namespace='Accounting')),
url(r'^payroll/', include('payroll.urls', namespace='payroll')),
url(r'^bill/', include('bill.urls', namespace='bill')),
url(r'^docs/', include('rest_framework_docs.urls')),
url(r'^proc/', include('procurement.urls', namespace='procurement')),
# url(r'^accounts/', include('organizations.urls')),
# url(r'^authority/', include('authority.urls')),
url(r'^organization/', include('organizations.urls')),
url(r'^admin/', admin.site.urls),
# url(r'^invitations/', include(invitation_backend().get_urls())),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Django Rest Framework URL Mapping for multiple apps

I have a django-rest project called main and under it I have created an app called users. So, my project has the files :-
main/main/urls.py
and
main/users/urls.py
In users/urls.py I have
from django.conf.urls import url, include
from rest_framework import routers
from users import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
and in the main/main/urls.py I have
from django.conf.urls import url
from django.contrib import admin
from users import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users.urls),
]
However, I keep getting the error NameError: name 'users' is not defined. What is the correct way to set up urls when I have multiple apps? I would like to have a urls.py file for each app that is independent of the project. And in the root urls.py would include routing to different apps.
You import url not user, can try it
from users import urls as users_url
# ^^^^^^^^
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users_url),
# ^^^^^^^
]
but better:
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', include('users.url')),
# ^^^^^^^
]
more details including-other-urlconfs

TypeError: view must be a callable or a list/tuple in the case of include()

I am new to django and python. During url mapping to views i am getting following error:
TypeError: view must be a callable or a list/tuple in the case of include().
Urls. py code:-
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/$', "posts.views.post_home"), #posts is module and post_home
] # is a function in view.
views.py code:-
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
#function based views
def post_home(request):
response = "<h1>Success</h1>"
return HttpResponse(response)
Traceback
In 1.10, you can no longer pass import paths to url(), you need to pass the actual view function:
from posts.views import post_home
urlpatterns = [
...
url(r'^posts/$', post_home),
]
Replace your admin url pattern with this
url(r'^admin/', include(admin.site.urls))
So your urls.py becomes :
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^posts/$', "posts.views.post_home"), #posts is module and post_home
]
admin urls are callable by include (before 1.9).
For Django 1.11.2
In the main urls.py write :
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/', include("Post.urls")),
]
And in the appname/urls.py file write:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.post_home),
]
Answer is in project-dir/urls.py
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
Just to complement the answer from #knbk, we could use the template below:
as is in 1.9:
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls), #it's not allowed to use the include() in the admin.urls
url(r'^posts/$', include(posts.views.post_home),
]
as should be in 1.10:
from your_project_django.your_app_django.view import name_of_your_view
urlpatterns = [
...
url(r'^name_of_the_view/$', name_of_the_view),
]
Remember to create in your_app_django >> views.py the function to render your view.
You need to pass actual view function
from posts.views import post_home
urlpatterns = [
...
url(r'^posts/$', post_home),
]
This works fine!
You can have a read at URL Dispatcher Django
and here Common Reguler Expressions Django URLs

How to solve geodjango url conflict for geojson data?

I have the urls
url(r'^geojson.data/$', geojson_provinces, name='geojson_provinces'),
url(r'^data.data/$', GeoJSONLayerView.as_view(model=Account,properties=('species','listvalues',)), name='data'),
url(r'^extdata.data/$', existingData_APFT, name='existingData_APFT'),
url(r'^data.data/$', GeoJSONLayerView.as_view(model=Existing_Data_APFT,properties=('species','listvalues',)), name='extdata'),
FRom the two urls Im displaying different geojson data on map. But When I run
http://localhost:8000/extdata.data/
The geojson data of (geojson.data) is getting dispalyed. I read from the docs that django checks the order but can it be done order independent?
urls.py
from django.conf.urls import url,include,patterns
from django.contrib.gis import admin
from pft.views import*
from django.conf import settings
from django.conf.urls.static import static
from pft.models import Account,Existing_Data_APFT
from django.views.generic import TemplateView
from djgeojson.views import GeoJSONLayerView
urlpatterns =patterns('',
url(r'^admin/', admin.site.urls),
#url(r'',include('pft.urls')),
url(r'^$', 'django.contrib.auth.views.login'),
url(r'^logout/$', logout_page),
url(r'^accounts/login/$', 'django.contrib.auth.views.login'), # If user is not login it will redirect to login page
url(r'^register/$', register),
url(r'^register/success/$', register_success),
url(r'^home/$', home),
url(r'^$',geojson),
url(r'^index/',index),
url(r'^submit', submit),
url(r'^pft',pft),
url(r'^geojson.data/$', geojson_provinces, name='geojson_provinces'),
url(r'^data.data/$', GeoJSONLayerView.as_view(model=Account,properties=('species','listvalues',)), name='data'),
url(r'^extdata.data/$', existingData_APFT, name='existingData_APFT'),
url(r'^data.data/$', GeoJSONLayerView.as_view(model=Existing_Data_APFT,properties=('species','listvalues',)), name='extdata'),
url(r'^potpft.data/$', TemplateView.as_view(template_name='pot_pft.html'), name='extract_raster_points'),
url(r'^datapotg.geojson/$', GeoJSONLayerView.as_view(model=Account,properties=('species','rastvalues',)), name='datapotg'),
url(r'^csv',add_multiple_accounts,name='add_multiple_accounts'),
url(r'^traits',add_traits,name='add_traits'),
(r'^', include('pft.urls')),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)