I have a website developped in django, which consist of around 25000 suburls. I need something to list all the urls in website and check if the links are broken periodically so i prefer to do something that i can run as a script.
Which method should i follow? Any idea ?
In Django 2.2.x, I had to use this slightly modified version of @sneawo's excellent answer:
from django import test
from django.urls import reverse, URLPattern
from myapp.urls import urlpatterns
class MyAppUrlsTest(test.SimpleTestCase):
def test_responses(self):
for url in urlpatterns:
# For now, perform only GET requests and ignore URLs that need arguments.
if not isinstance(url, URLPattern) or url.pattern.regex.groups or not url.name:
continue
urlpath = reverse(url.name)
response = self.client.get(urlpath, follow=True)
self.assertEqual(response.status_code, 200)
Note that I'm also accounting for views that require arguments by just ignoring them. For my specific, simplistic use case, this also lets me exclude views by not giving them a name
in my urlpatterns
.
Also see https://github.com/encode/django-rest-framework/pull/5500#issue-146618375.
Use show-urls command in django-extensions as a starting point. (documentation)
python manage.py show_urls
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With