[英]Using mongodb in django — how to form url patterns
I am going through the django tutorial on creating your first app. 我正在阅读有关创建第一个应用程序的django教程。 I am using MongoDB as a backend.
我正在使用MongoDB作为后端。 At part 3 , I had an issue with the url patterns, which I was able to resolve by using this:
在第3部分中 ,我对url模式有问题,我可以使用以下方法解决:
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^([0-9a-fA-F]{24})/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^([0-9a-fA-F]{24})/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^([0-9a-fA-F]{24})/vote/$', views.vote, name='vote'),
) )
At part 4 , I have a similar issue with this code: 在第4部分中 ,此代码存在类似的问题:
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
I tried this: 我尝试了这个:
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^([0-9a-fA-F]{24})/$', views.DetailView.as_view(), name='detail'),
url(r'^([0-9a-fA-F]{24})/results/$', views.ResultsView.as_view(), name='results'),
url(r'^([0-9a-fA-F]{24})/vote/$', views.vote, name='vote'),
but I was not able to get the proper view. 但我无法获得正确的看法。 I get this error:
我收到此错误:
AttributeError at /polls/53286e672da57ad4f09297a6/
Generic detail view DetailView must be called with either an object pk or a slug.
I assume that these views are looking for the or tag, but mongo's ids are not like RMDBs, so it fails if you don't change it. 我假设这些视图正在寻找or标记,但是mongo的ID与RMDB不同,因此如果不进行更改,它将失败。
How do I get mongo to work with their code? 我如何让mongo使用其代码?
Figured it out... from https://django-mongodb-engine.readthedocs.org/en/latest/tutorial.html#adding-some-templates 从https://django-mongodb-engine.readthedocs.org/en/latest/tutorial.html#adding-some-templates中找出来...
Make the url patterns like this: 使网址格式如下所示:
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[a-z\d]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[a-z\d]+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<poll_id>[a-z\d]+)/vote/$', views.vote, name='vote'),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.