在 Django 中,使用美丽的 url 会更容易。非常感谢!但我用纯 php 编写了一个网站,现在我想迁移到 Python 和 Django,并且仍然想保留我丑陋的 url。 (丑 > 404)
如何处理如下网址: http://site.domain/?user=12
这是我的 urls.py:
urlpatterns = [
url(r'^\?user=(\d+)/$', user),
]
我的观点.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 中获取参数。
关于python - django 如何使用丑陋的网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35551720/