存在 Django 和测试本地 URL

发布于 2024-09-13 12:09:11 字数 1291 浏览 9 评论 0原文

我有一个模型需要存储将成为 Django 环境一部分的 URL。如果我存储普通 URL,我会使用 models .URLField,并使用 verify_exists 确保 URL 确实存在。

然而,这在开发中效果并不好,因为开发服务器是单线程的,它会无限期地挂起,因为它无法同时处理两个请求。

我希望使用 resolve(),但我很难将该文档页面末尾的函数 myview 调整为不接受请求的版本,因为我想检查给定的本地 URL 是否可以被解决,并在没有引发 404 的情况下从空白会话中调用。

我希望使用 validator 来完成此操作,如下所示:

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)

但是,如果没有将有效的 request 对象传递到 kwargs 中,则会失败。如何生成虚拟(空白)请求对象?我尝试过仅使用 django.http.HttpRequest 。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

行雁书 2024-09-20 12:09:11

只是一个疯狂的想法,不确定是否有帮助。您是否考虑过命名网址并使用 reverse()< /代码>?如果 URL 有效,Reverse 就会起作用;如果 URL 无效,Reverse 就会失败。

Just a wild idea, not sure if it will be helpful. Have you considered naming the urls and using reverse()? Reverse will work if the URL is valid and will fail when it is not.

大海や 2024-09-20 12:09:11

您是否喜欢使用 django 测试客户端?

如果是这样,这应该可以做到:

from django.test.client import Client

def validate_local_url(path):
    c = Client()
    try:
        resp = c.get(path)
        if resp.status_code == 404:
            raise ValidationError(u'%s is not a local URL (does not exist)' % value)
    except:
        raise ValidationError(u'%s is not a local URL (not a valid URL)' % value)

只是,你知道,确保在死刑下 validate_local_url 不能被本地 GET 调用。请求,否则有人可以轻松地将您的服务器设置为无限循环:

# urls.py
url('^infinite_loop/

然后:

http://example.com com/infinite_loop/?path_field=infinite_loop

, 'myapp.infinite_loop', 'infinite_loop') #views.py def infinite_loop_view(request, template_name="blah.html", form_class=MyForm): my_form = form_class(request.REQUEST or None) # yes, admittedly this is dumb if my_form.is_valid(): return HttpResponse("Congratulations! Your path was totally valid.") return render_to_response(template_name, locals(), RequestContext(request))

然后:

http://example.com com/infinite_loop/?path_field=infinite_loop

Are you cool with using the django test Client?

If so, this should do it:

from django.test.client import Client

def validate_local_url(path):
    c = Client()
    try:
        resp = c.get(path)
        if resp.status_code == 404:
            raise ValidationError(u'%s is not a local URL (does not exist)' % value)
    except:
        raise ValidationError(u'%s is not a local URL (not a valid URL)' % value)

Just, you know, make sure under penalty of death that validate_local_url can never be called by a local GET request, otherwise someone can trivially set your server on an infinite loop:

# urls.py
url('^infinite_loop/

And then:

http://example.com/infinite_loop/?path_field=infinite_loop

, 'myapp.infinite_loop', 'infinite_loop') #views.py def infinite_loop_view(request, template_name="blah.html", form_class=MyForm): my_form = form_class(request.REQUEST or None) # yes, admittedly this is dumb if my_form.is_valid(): return HttpResponse("Congratulations! Your path was totally valid.") return render_to_response(template_name, locals(), RequestContext(request))

And then:

http://example.com/infinite_loop/?path_field=infinite_loop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文