Want to generate Custom 404/500 error page in django app - django

This error comes by default and I want to customize it
I have tried the code bellow to generate custom error/404/500 page but its not working at all,
actually I want to raise this error when user tries to enter wrong url,then a custom error page with 404/500 would be displayed to the user.
views.py
from django.shortcuts import render
from django.shortcuts import render
from django.conf import settings
def error_404(request,exception):
context = {}
context = {"project name":settings.PROJECT_NAME}
return render(request,'error_404.html',context)
def error_500(request):
context = {}
context = {"project name":settings.PROJECT_NAME}
return render(request,'error_500.html',context)
app urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from django.conf.urls import handler404, handler500, include
from .import views
import testapp
from testapp import views as common_views
urlpatterns = [
path('error_404',views.error_404,'error_404'),
path('error_500',views.error_500,'error_500'),
]
project/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import handler404, handler500, include
from testapp import urls
import testapp
from testapp import views as common_views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(testapp.urls)),
]
handler404 = common_views.error_404
handler500 = common_views.error_500

It will work when you make DEBUG = False, and do not forget to add ALLOWED_HOSTS = ['localhost', '127.0.0.1']

Related

Django DRF APITestCase post url results in 404 error

I'm learning DRF test cases, and in my test.py file, my URL in the client post-call is coming back in a 400 status error:
Here's my urls.py:
from django.contrib import admin
from django.urls import path, include
#from rest_auth.views import LoginView, LogoutView
urlpatterns = [
path('admin/', admin.site.urls),
path("api/", include("profiles.api.urls")),
path("api-auth/", include("rest_framework.urls")),
path("api/rest-auth/", include("rest_auth.urls")),
path("api/rest-auth/registration/", include("rest_auth.registration.urls"))
]
from django.conf.urls.static import static
from django.conf import settings
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Here's my test.py file
import json
from django.contrib.auth.models import User
from django.urls import reverse
from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase
from rest_framework import status
from profiles.models import Profile
from profiles.api.serializers import ProfileSerializer
class RegistrationTestCase(APITestCase):
def test_registration(self):
data = {"username": "testuser1", "email": "test#localhost.app", "password1": "A41&14all", "password2": "A41#14all"}
response = self.client.post("/api/rest-auth/registration/", data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
It appears that the line self.client.post.... fails to find the endpoint.
What am I missing?
thanks!

does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import .Django 3.0.2

Project name : CRMPROJECT
App name: leads
I'm working on a CRM project With Django and I keep getting this error while trying to map my urls to views
django.core.exceptions.ImproperlyConfigured. The included URLconf does not appear to have any patterns in it
Below is the project/urls.py file
'''
from django.contrib import admin
from django.urls import path,include
from leads import views
urlpatterns = [
path('admin/', admin.site.urls),
path('leads/',include('leads.urls',namespace='leads')),
]
'''
Below is leads/urls.py
from django.urls import path
from . import views
app_name = 'leads'
urlpatterns = [
path('',views.home,name='home'),
]
And lastly my leads/views.py
'''
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return render(request,'leads/base.html')
'''

Getting error : 'admin' is not a registered namespace, on trying to access the password reset view

I created a password reset view following this tutorial.
I used the urls for class based views since django uses CBVs in the version 2.1 and above. It seemed to work fine, but today I am getting the above stated error. I have commented the admin url, on uncommenting it the password_reset view works, but through the django registration and uses it's own template regardless of the templates I have created. Why am I getting this problem suddenly when it worked fine earlier?
urls.py
from django.conf.urls import url,include
from django.contrib import admin
from NewApp import views
from django.conf import settings
from django.conf.urls.static import serve
from django.urls import path
urlpatterns = [
url(r'^$', views.user_login , name='user_login'),
url(r'^NewApp/', include('NewApp.urls', namespace="NewApp")),
path('', include('django.contrib.auth.urls')),
]
NewApp/urls.py
from django.conf.urls import url,include
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth import views as auth_views
app_name = 'NewApp'
urlpatterns = [
path('password_reset/', auth_views.PasswordResetView.as_view(), name='password_reset'),
path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]
Apart from these, I have only used templates which are just copied from the link I have mentioned.
views for password reset need to access the admin by default. So you have to include the admin site to urls.py:
from django.urls import path
from django.contrib import admin
urlpatterns = [
...
path('admin/', admin.site.urls),
...
]

page not found error in Django using a filter query {return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')}

I am making my first django practice project I have a view called PostListview :
from django.shortcuts import render
from django.utils import timezone
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required
from blog.models import Post, Comment
from blog.forms import PostForm,CommentForm
from django.urls import reverse_lazy
from django.views.generic import(TemplateView,ListView,DetailView,CreateView,UpdateView,
DeleteView)
class AboutView(TemplateView):
template_name = 'about.html'
class PostListView(ListView):
model = Post
def get_queryset(self):
return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
I have this in in the urls:
from django.conf.urls import url
from blog import views
urlpatterns = [
url(r'^$',views.PostListView.as_view(), name= 'post_list'),
And this is the template which is calling this view.
<li class="navbar-brand bigbrand" >My Tech Blog</li>
This view is also set as the default home view and it opens fine at the time i open the site url (localhost) but when I am clicking "My Tech blog " it gives a 404 error.
This is the main urls.py :
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from django.contrib.auth import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('blog.urls')),
url(r'accounts/login/$',views.login, name = 'login'),
url(r'accounts/logout/$',views.logout, name = 'logout',kwargs={'next_page':'/'}),
]
Just change
url(r'^$',views.PostListView.as_view(), name= 'post_list'),
with
url(r'',views.PostListView.as_view(), name= 'post_list'),

Django Name Error

I have the following code in my urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^testModule/',include(testModule.urls)),
url(r'^admin/', admin.site.urls),
]
and testModule is my app name which includes :
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index,name='index'),
]
And my views.py is
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello")
# Create your views here.
However, i get the following error while running the server:
line 20: url(r'^testModule/',include(testModule.urls)),
NameError: name 'testModule' is not defined
You haven't imported testModule in your main urls.
Had the same problem but solved it like this:
add "import testModule" to your urls.py
add "from django.conf.urls import url" to your urls.py in the app directory, in this case testModule
Once done you will get a "Page not found at/" error when you reload 127.0.0.1:8000. This is bacause the url to your app is actually at 127.0.0.1:8000/testModule