2
在Django中,使用美麗的網址更容易。非常感謝!但是我用純php編寫了一個網站,現在我想遷移到Python和Django,並且仍然想讓我的醜陋的Url活着。 (醜> 404)django如何與醜陋的網址一起工作
我怎麼能處理像URL: 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
有人可以幫我解決這個問題嗎? 任何教程或網址是欣賞。
'?user = 12' chars after to'? '是參數。 –
但它說: 「?^用戶=(\ d +)/ $」不是一個有效的正則表達式 有正則表達式 –
「沒有重複」的問題表明了一個問題「?」。你確定你在你的實際代碼中逃脫了嗎? – Selcuk