2

我有这个 url http://127.0.0.1:8000/upload/picturelist/1,这使得 user_id = 1,

在我的 urls.py

url(r'^picturelist/(?P<user_id>\d+)$', views.pictureList),

在我看来.py

def pictureList(request, user_id):
    if int(user_id) != request.user.id:
        raise PermissionDenied

如何使这个基于函数的视图使用 createview?

class pictureList(CreateView):
4

2 回答 2

0

我从未使用过CreateView,但这是我从阅读文档中收集到的内容:

您可以通过定义form_valid来做到这一点:

看法:

class pictureList(CreateView):
  model = YourModelHere
  fields = ['whatever','fields','you','want','edited']

  def form_valid(self, form):
    record = form.save(commit = False)

    # assuming the user id is associated 
    # to the model with fieldname user_id

    if (self.request.user == record.user_id):
      record.save()
      return HttpResponseRedirect(self.get_success_url())
    # not sure if this works:
    return self.form_invalid()

然后模板将位于'yourappname/yourmodelhere_form.html'.

有关示例,请参见CreateView 。

于 2014-01-25T03:36:11.130 回答
0

你可以这样做:

在 urls.py 中:url(r'^picturelist/(?P<user_id>\d+)$', views.MakeItView.as_view()),

在 vi​​ews.py 中:

class MakeItView(CreateView):
    model = myModel
    template_name = 'whatever.html'

    def get_context_data(self, **kwargs):
        context = super(MakeItView, self).get_context_data(**kwargs)
        if int(self.kwargs['user_id']) != self.request.user.id:
            raise PermissionDenied
        return context
于 2014-01-25T04:34:16.637 回答