在Django URL中检索作为参数的IP地址

时间:2022-03-30 10:29:25

My application is listing some game servers IP addresses.

我的应用程序列出了一些游戏服务器的IP地址。

I want to add a simple search engine, taking a regular expression in it. I would type ^200. to list only the IP addresses beginning with 200.

我想添加一个简单的搜索引擎,其中包含一个正则表达式。我将类型^ 200。只列出以200开头的IP地址。

The form would redirect me to the results page by sending a GET request like that :

该表单通过发送这样的GET请求将我重定向到结果页面:

/servers/search/^200./page/1/range/30/

This is the line I'm using in urls.py :

这是我在url中使用的行。py:

url(r'^servers/search/(?P<search>[a-zA-Z0-9.]+)/page/(?P<page>\d+)/range/(?P<count>\d+)/$', gameservers.views.index)

But it doesn't work the way I expected. No results are shown. I've intentionally made a syntax error to see the local variables. Then I realized that the search variable's value is the following :

但这并不是我期望的那样。没有结果显示。我故意犯了一个语法错误来查看本地变量。然后我意识到搜索变量的值如下:

^200./page/1/range/30/

How can I fix this ? I've thought about moving the search parameter in the url's ending, but it might be very interesting to see if there is a way to limit the value with the next /.

我该怎么解决这个问题呢?我已经考虑过在url的结尾移动搜索参数,但是看看是否有一种方法可以用next /来限制值,这可能会很有趣。

1 个解决方案

#1


3  

Your regex doesn't match at all: you are not accepting the ^ character. But even if it was, there's no way that the full URL could all be captured in the search variable, because then the rest of the URL wouldn't match.

你的正则表达式不匹配:你不接受^字符。但即使是这样,也不可能在search变量中捕获完整的URL,因为这样URL的其余部分就不匹配了。

However, I wouldn't try to fix this. Trying to capture complicated patterns in the URL itself is usually a mistake. For a search value, it's perfectly acceptable to move that to a GET query parameter, so that your URL would look something like this:

但是,我不会试图解决这个问题。试图在URL中捕获复杂的模式通常是错误的。对于搜索值,将其移动到GET查询参数是完全可以接受的,这样您的URL就会像这样:

/servers/search/?search=^200.&page=1&range=30

or, if you like, you could still capture the page and range values in the URL, but leave the search value as a query param.

或者,如果您愿意,也可以在URL中捕获页面和范围值,但将搜索值保留为查询参数。

#1


3  

Your regex doesn't match at all: you are not accepting the ^ character. But even if it was, there's no way that the full URL could all be captured in the search variable, because then the rest of the URL wouldn't match.

你的正则表达式不匹配:你不接受^字符。但即使是这样,也不可能在search变量中捕获完整的URL,因为这样URL的其余部分就不匹配了。

However, I wouldn't try to fix this. Trying to capture complicated patterns in the URL itself is usually a mistake. For a search value, it's perfectly acceptable to move that to a GET query parameter, so that your URL would look something like this:

但是,我不会试图解决这个问题。试图在URL中捕获复杂的模式通常是错误的。对于搜索值,将其移动到GET查询参数是完全可以接受的,这样您的URL就会像这样:

/servers/search/?search=^200.&page=1&range=30

or, if you like, you could still capture the page and range values in the URL, but leave the search value as a query param.

或者,如果您愿意,也可以在URL中捕获页面和范围值,但将搜索值保留为查询参数。