Advertisement
horozov86

Change 404 to 403

Mar 27th, 2024
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. 1.
  2.  
  3. # views.py
  4. from django.shortcuts import render
  5.  
  6. def error_403(request, exception):
  7.     return render(request, '403.html', status=403)
  8.  
  9. # urls.py
  10. from django.urls import path, include
  11. from .views import error_403
  12.  
  13. urlpatterns = [
  14.     # your other paths
  15. ]
  16.  
  17. handler403 = 'my_holiday.destination.views.error_403'
  18.  
  19.  
  20. 2.
  21.  
  22. <!DOCTYPE html>
  23. <html lang="en">
  24. <head>
  25.     <meta charset="UTF-8">
  26.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  27.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  28.     <title>Forbidden (403)</title>
  29.  
  30.     <style>
  31.         body {
  32.             font-family: Arial, sans-serif;
  33.             background-color: #f0f0f0;
  34.             text-align: center;
  35.             padding: 50px;
  36.         }
  37.         h1 {
  38.             color: #ff0000; /* Change color to red */
  39.         }
  40.         img {
  41.             max-width: 100%;
  42.             height: auto;
  43.             margin-bottom: 20px;
  44.         }
  45.     </style>
  46. </head>
  47. <body>
  48.     <h1>{{ message }}</h1>
  49.     <img src="{% static 'images/403-status-code.png' %}" alt="Forbidden">
  50.  
  51. </body>
  52. </html>
  53.  
  54. 3.
  55.  
  56. from django.http import HttpResponseForbidden
  57.  
  58. @method_decorator(login_required, name='dispatch')
  59. class PlaceEditView(views.UpdateView):
  60.     queryset = Place.objects.all()
  61.     template_name = "destination/place_edit.html"
  62.     form_class = PlaceEditForm
  63.     success_url = reverse_lazy('travelogue_view')
  64.  
  65.     def dispatch(self, request, *args, **kwargs):
  66.         obj = self.get_object()
  67.         if obj.user != self.request.user:
  68.             return HttpResponseForbidden("You do not have permission to edit this place.")
  69.         return super().dispatch(request, *args, **kwargs)
  70.  
  71. @method_decorator(login_required, name='dispatch')
  72. class PlaceDeleteView(views.DeleteView):
  73.     queryset = Place.objects.all()
  74.     template_name = "destination/place_delete.html"
  75.     success_url = reverse_lazy('travelogue_view')
  76.  
  77.     def dispatch(self, request, *args, **kwargs):
  78.         obj = self.get_object()
  79.         if obj.user != self.request.user:
  80.             return HttpResponseForbidden("You do not have permission to delete this place.")
  81.         return super().dispatch(request, *args, **kwargs)
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement