Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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基于函数的视图到基于类的视图_Python_Django_Class_Django Generic Views_Create View - Fatal编程技术网

Python Django基于函数的视图到基于类的视图

Python Django基于函数的视图到基于类的视图,python,django,class,django-generic-views,create-view,Python,Django,Class,Django Generic Views,Create View,我知道这个url是http://127.0.0.1:8000/upload/picturelist/1,使用户id=1 在myurl.py中 url(r'^picturelist/(?P<user_id>\d+)$', views.pictureList), 如何使此基于函数的视图使用createview class pictureList(CreateView): 我从未使用过CreateView,但以下是我从阅读文档中获得的信息: 您可以通过定义: 查看: class pic

我知道这个url是
http://127.0.0.1:8000/upload/picturelist/1
,使用户id=1

在myurl.py中

url(r'^picturelist/(?P<user_id>\d+)$', views.pictureList),
如何使此基于函数的视图使用createview

class pictureList(CreateView):

我从未使用过
CreateView
,但以下是我从阅读文档中获得的信息:

您可以通过定义:

查看:

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\u form.html”


请参见示例。

您可以执行以下操作:

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


在views.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

嗨,我在/author/add/4上找到了NoReverseMatch。
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