Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list all suburls and check if broken in python

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 ?

like image 348
tuna Avatar asked Jan 22 '13 08:01

tuna


2 Answers

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.

like image 106
pradeepcep Avatar answered Oct 13 '22 23:10

pradeepcep


Use show-urls command in django-extensions as a starting point. (documentation)

python manage.py show_urls
like image 22
ustun Avatar answered Oct 13 '22 23:10

ustun