Django Url 不匹配找不到页面
Django Url not matching Page no found
我有一个 url 即
http://127.0.0.1:8000/users/profiles/?profile=17320
我的 urls.py 文件中定义了这一行。
url(r'^profiles/(?P<profile>\d+)/$', views.display_profile),
和 运行,点击上面的 url,它没有打开页面,实际上显示所有可用的 urls 列表。还有 Page not found (404)
你的正则表达式匹配
http://127.0.0.1:8000/profiles/17320
没有
http://127.0.0.1:8000/users/profiles/?profile=17320
所以应该修复。还获取参数(如?profile=..)未在 django url 中捕获。
伙计,查询字符串和作为参数的 url 部分存在差异。
您正在做的是查询字符串,并且您已经为 url 的一部分设置了 url 模式作为参数。
所以你的 url 应该是 http://127.0.0.1:8000/users/profiles/17320/
阅读更多内容
您的 url 与此类似
127.0.0.1:8000/profiles/542/
如果您想将配置文件 ID 用作 GET 参数,而不是更改您的 urls 以匹配
url(r'^profiles/$', views.display_profile),
在你的视图函数中或在 get(self, request, *args, **kwargs)
方法中的基于 class 的视图中使用
获取你的参数
profile_id = request.GET.get('profile', None) # where None is the value to use
# if profile does not exist
希望对您有所帮助
我之前写了另一个答案来回答你完全相同的问题
How to access url part in python django?
我有一个 url 即
http://127.0.0.1:8000/users/profiles/?profile=17320
我的 urls.py 文件中定义了这一行。
url(r'^profiles/(?P<profile>\d+)/$', views.display_profile),
和 运行,点击上面的 url,它没有打开页面,实际上显示所有可用的 urls 列表。还有 Page not found (404)
你的正则表达式匹配
http://127.0.0.1:8000/profiles/17320
没有
http://127.0.0.1:8000/users/profiles/?profile=17320
所以应该修复。还获取参数(如?profile=..)未在 django url 中捕获。
伙计,查询字符串和作为参数的 url 部分存在差异。
您正在做的是查询字符串,并且您已经为 url 的一部分设置了 url 模式作为参数。
所以你的 url 应该是 http://127.0.0.1:8000/users/profiles/17320/
您的 url 与此类似
127.0.0.1:8000/profiles/542/
如果您想将配置文件 ID 用作 GET 参数,而不是更改您的 urls 以匹配
url(r'^profiles/$', views.display_profile),
在你的视图函数中或在 get(self, request, *args, **kwargs)
方法中的基于 class 的视图中使用
profile_id = request.GET.get('profile', None) # where None is the value to use
# if profile does not exist
希望对您有所帮助
我之前写了另一个答案来回答你完全相同的问题
How to access url part in python django?