31 lines
903 B
Python
31 lines
903 B
Python
from django.views.generic import DetailView
|
|
from django.shortcuts import redirect
|
|
from django.core.validators import URLValidator
|
|
from django.core.exceptions import ValidationError
|
|
from django.http import HttpResponseNotFound
|
|
from .models import Page
|
|
|
|
# Create your views here.
|
|
|
|
|
|
class PageView(DetailView):
|
|
queryset = Page.objects.filter(visible=True)
|
|
slug_field = "url"
|
|
template_name = "page.html"
|
|
|
|
def render_to_response(self, context, **response_kwargs):
|
|
page = self.object
|
|
|
|
if self.object.kind == Page.REDIRECT:
|
|
target = str(page.content)
|
|
|
|
validate_url = URLValidator()
|
|
try:
|
|
validate_url(page.content)
|
|
except ValidationError:
|
|
return HttpResponseNotFound()
|
|
|
|
return redirect(page.content, permanent=True)
|
|
|
|
return super().render_to_response(context, **response_kwargs)
|