Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django如何使用丑陋的url_Python_Django_Web_Frameworks - Fatal编程技术网

Python django如何使用丑陋的url

Python django如何使用丑陋的url,python,django,web,frameworks,Python,Django,Web,Frameworks,在Django中,使用漂亮的URL更容易。好的,谢谢!但是我用纯php编写了一个网站,现在我想迁移到Python和Django,并且仍然想让我丑陋的URL保持活力。(丑陋>404) 我如何处理URL,如: 这是我的URL.py: urlpatterns = [ url(r'^\?user=(\d+)/$', user), ] 和my views.py: def user(request, offset): try: offset

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

我如何处理URL,如:

这是我的URL.py:

urlpatterns = [
        url(r'^\?user=(\d+)/$', user),
]
和my views.py:

def user(request, offset):
        try:
                offset = int(offset)
        except ValueError:
                raise Http404()
        ret = 'hello user ' + str(offset)
        return HttpResponse(ret)
但我在运行服务器时遇到此错误(其他页面工作正常):

正在执行系统检查。。。
由启动的线程中存在未处理的异常
回溯(最近一次呼叫最后一次):
regex中的文件“/home/pouya/dj/helloworld/lib/python3.5/site packages/django/core/urlresolvers.py”,第199行
compiled_regex=re.compile(regex,re.UNICODE)
文件“/home/pouya/dj/helloworld/lib/python3.5/re.py”,编译中的第224行
返回编译(模式、标志)
文件“/home/pouya/dj/helloworld/lib/python3.5/re.py”,第293行,在编译中
p=sre_compile.compile(模式、标志)
compile中的文件“/home/pouya/dj/helloworld/lib/python3.5/sre_compile.py”,第536行
p=sre_parse.parse(p,标志)
文件“/home/pouya/dj/helloworld/lib/python3.5/sre_parse.py”,第829行,在parse中
p=_parse_sub(源,模式,0)
文件“/home/pouya/dj/helloworld/lib/python3.5/sre_parse.py”,第437行,在
itemsappend(_解析(源、状态))
文件“/home/pouya/dj/helloworld/lib/python3.5/sre_parse.py”,第638行,in_parse
source.tell()-here+len(this))
sre_constants.error:在位置1没有要重复的内容
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
包装器中的文件“/home/pouya/dj/helloworld/lib/python3.5/site packages/django/utils/autoreload.py”,第226行
fn(*args,**kwargs)
文件“/home/pouya/dj/helloworld/lib/python3.5/site packages/django/core/management/commands/runserver.py”,第116行,在内部运行
self.check(display\u num\u errors=True)
文件“/home/pouya/dj/helloworld/lib/python3.5/site packages/django/core/management/base.py”,第426行,检查中
包括部署检查=包括部署检查,
文件“/home/pouya/dj/helloworld/lib/python3.5/site packages/django/core/checks/registry.py”,第75行,运行检查
新建错误=检查(应用程序配置=应用程序配置)
文件“/home/pouya/dj/helloworld/lib/python3.5/site packages/django/core/checks/url.py”,第10行,在check\u url\u配置中
返回检查\u分解器(分解器)
文件“/home/pouya/dj/helloworld/lib/python3.5/site packages/django/core/checks/url.py”,第27行,在check_解析器中
警告.扩展(用斜杠(模式)检查模式启动)
文件“/home/pouya/dj/helloworld/lib/python3.5/site packages/django/core/checks/url.py”,第63行,在check_pattern_startswith_斜线中
regex_pattern=pattern.regex.pattern
regex中的文件“/home/pouya/dj/helloworld/lib/python3.5/site packages/django/core/urlresolvers.py”,第203行
(regex,six.text_type(e)))
django.core.exceptions.ImpropertlyConfigured:“^?user=(\d+/$”不是有效的正则表达式:在位置1没有可重复的内容
有人能帮我解决这个问题吗?
非常感谢任何教程或url。

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

    url(r'^$', user),

您应该通过
请求在视图中获取参数。获取['user']

?user=12
字符后到
是参数。但它表示:“^?user=(\d+)/$”不是有效的正则表达式正则表达式有问题“无需重复”表示“?”有问题。你确定你在实际代码中逃过了吗?谢谢,真的很有帮助。
    url(r'^$', user),