在django中使用mongodb - 如何形成url模式

时间:2014-03-18 19:04:07

标签: python django mongodb

我正在浏览有关创建第一个应用程序的django教程。我使用MongoDB作为后端。在part 3,我遇到了网址模式的问题,我可以通过使用它解决这个问题:

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'),

part 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'),
)

我试过了:

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'),

但我无法得到正确的观点。我收到这个错误:

AttributeError at /polls/53286e672da57ad4f09297a6/
Generic detail view DetailView must be called with either an object pk or a slug.

我认为这些视图正在寻找或标记,但mongo的id不像RMDB,所以如果你不改变它就会失败。

如何让mongo使用他们的代码?

1 个答案:

答案 0 :(得分:0)

https://django-mongodb-engine.readthedocs.org/en/latest/tutorial.html#adding-some-templates

计算出来......

制作这样的网址模式:

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'),