I have a model which needs to store URLs which will be part of the Django environment. If I was storing normal URLs, I'd use models.URLField
, and use verify_exists
to make sure the URL actually exists.
However, this doesn't work that great in development, since the dev server is single-threaded, it hangs indefinitely, since it can't process two requests at once.
I was hoping to do something using resolve()
, but am having difficulty adapting the function myview
at the end of that documentation page to a version which does not take a request, since I want to check that a given local URL can be resolved, and called without a 404 being raised, from a blank session.
I was hoping to do this with a validator, something like this:
def validate_local_url(value):
try:
view, args, kwargs = resolve(value)
view(*args, **kwargs)
except Resolver404:
raise ValidationError(u'%s is not a local URL (not a valid URL)' % value)
except Http404:
raise ValidationError(u'%s is not a local URL (does not exist)' % value)
However, this fails without a valid request
object being passed into kwargs
. How do I generate a dummy (blank) request object? I've tried just using django.http.HttpRequest
.