Skip to main content
 首页 » 编程设计

python之Django 如何使用丑陋的网址

2024年10月24日22arxive

在 Django 中,使用 Beautiful urls 更容易。非常感谢!但是我用纯 php 编写了一个网站,现在我想迁移到 Python 和 Django,并且仍然想让我丑陋的 url 保持活力。 (丑 > 404)

我如何处理如下网址: http://site.domain/?user=12

这是我的 urls.py:

urlpatterns = [ 
        url(r'^\?user=(\d+)/$', user), 
] 

还有我的views.py:

def user(request, offset): 
        try: 
                offset = int(offset) 
        except ValueError: 
                raise Http404() 
        ret = 'hello user ' + str(offset) 
        return HttpResponse(ret) 

但是当我运行服务器时出现这个错误(其他页面工作正常):

Performing system checks... 
 
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f76199b6048> 
Traceback (most recent call last): 
  File "/home/pouya/dj/helloworld/lib/python3.5/site-packages/django/core/urlresolvers.py", line 199, in regex 
    compiled_regex = re.compile(regex, re.UNICODE) 
  File "/home/pouya/dj/helloworld/lib/python3.5/re.py", line 224, in compile 
    return _compile(pattern, flags) 
  File "/home/pouya/dj/helloworld/lib/python3.5/re.py", line 293, in _compile 
    p = sre_compile.compile(pattern, flags) 
  File "/home/pouya/dj/helloworld/lib/python3.5/sre_compile.py", line 536, in compile 
    p = sre_parse.parse(p, flags) 
  File "/home/pouya/dj/helloworld/lib/python3.5/sre_parse.py", line 829, in parse 
    p = _parse_sub(source, pattern, 0) 
  File "/home/pouya/dj/helloworld/lib/python3.5/sre_parse.py", line 437, in _parse_sub 
    itemsappend(_parse(source, state)) 
  File "/home/pouya/dj/helloworld/lib/python3.5/sre_parse.py", line 638, in _parse 
    source.tell() - here + len(this)) 
sre_constants.error: nothing to repeat at position 1 
 
During handling of the above exception, another exception occurred: 
 
Traceback (most recent call last): 
  File "/home/pouya/dj/helloworld/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper 
    fn(*args, **kwargs) 
  File "/home/pouya/dj/helloworld/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run 
    self.check(display_num_errors=True) 
  File "/home/pouya/dj/helloworld/lib/python3.5/site-packages/django/core/management/base.py", line 426, in check 
    include_deployment_checks=include_deployment_checks, 
  File "/home/pouya/dj/helloworld/lib/python3.5/site-packages/django/core/checks/registry.py", line 75, in run_checks 
    new_errors = check(app_configs=app_configs) 
  File "/home/pouya/dj/helloworld/lib/python3.5/site-packages/django/core/checks/urls.py", line 10, in check_url_config 
    return check_resolver(resolver) 
  File "/home/pouya/dj/helloworld/lib/python3.5/site-packages/django/core/checks/urls.py", line 27, in check_resolver 
    warnings.extend(check_pattern_startswith_slash(pattern)) 
  File "/home/pouya/dj/helloworld/lib/python3.5/site-packages/django/core/checks/urls.py", line 63, in check_pattern_startswith_slash 
    regex_pattern = pattern.regex.pattern 
  File "/home/pouya/dj/helloworld/lib/python3.5/site-packages/django/core/urlresolvers.py", line 203, in regex 
    (regex, six.text_type(e))) 
django.core.exceptions.ImproperlyConfigured: "^?user=(\d+)/$" is not a valid regular expression: nothing to repeat at position 1 

谁能帮我解决这个问题? 感谢任何教程或网址。

请您参考如下方法:

正如 Avinash 在评论中所说,? 之后的任何内容都是 GET 参数,它不是 URL 的一部分。你的 urlpattern 应该是:

    url(r'^$', user), 

您应该使用 request.GET['user'] 在您的 View 中获取参数。