lundi 22 février 2016

django how to work with ugly-url

In Django, it's easier to work with Beautiful urls. Well thanks! But I wrote a website with pure php and now I want to migrate to Python and Django and still want to keep my ugly urls alive. (ugly > 404)

How can I process urls like: http://ift.tt/1LC6TNw

here is my urls.py:

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

And my views.py:

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

but I get this error when I run the server (other pages work fine):

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

Can anybody help me with this issue? Any tutorial or url is appreciate.

Aucun commentaire:

Enregistrer un commentaire