在Django中,使用Beautiful urls更容易。非常感谢!但我写了一个纯PHP的网站,现在我想迁移到Python和Django,仍然希望保持我丑陋的网址活着。 (丑陋> 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
有人可以帮我解决这个问题吗? 任何教程或网址都很受欢迎。
答案 0 :(得分:2)
正如Avinash在评论中所说,?
之后的任何内容都是GET参数,它不是URL的一部分。你的urlpattern应该是:
url(r'^$', user),
您应该在视图中使用request.GET['user']
获取参数。