D
D
David52020-03-01 14:46:24
Django
David5, 2020-03-01 14:46:24

How to write an exception if there is no such url?

5e5ba03054263162351183.png

I know that it seems to be implemented through Resolver404, but I don’t know how to write it in the code
so that an exception is thrown if the url is incorrect

from django.shortcuts import render
from .forms import *
from .models import *
from django.http import HttpResponsePermanentRedirect
from django.core.cache import cache
import datetime

def index(request):
    

    form = AppForm(request.POST)
    if request.method == 'POST' and form.is_valid():
        

        ip = request.META.get('HTTP_X_FORWARDED_FOR', '')
        IP_KEY = 'request_ban:{ip}'
        DELAY = 43200

        
        result = cache.get(IP_KEY.format(ip=ip))
        if result:
            difference = ((result - datetime.datetime.now()).seconds)

           
            
            return HttpResponsePermanentRedirect("ban")
        else:
            cache.set(
                IP_KEY.format(ip=ip),
                datetime.datetime.now() + datetime.timedelta(seconds=DELAY),
                DELAY,
            )
            new_form = form.save()
            return HttpResponsePermanentRedirect("redirect")
        
        
    return render(request, 'remontsite/footer.html', context={'form': form})
    
    

def redirect(request):
    return render(request, 'remontsite/redirect.html')

def ban(request):
    return render(request, 'remontsite/ban.html')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-03-01
@bacon

1. they kick for import *
2. don't do form = AppForm(request.POST) before checking request.method == 'POST'
3. what is "wrong url", where exactly will the exception be thrown? no, we don’t want to guess
4. bring your Wishlist to the nginx level, it does it very well, the limit_req keyword

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question