English 中文(简体)
Django: 将论据传递到自定义观点类(遗产版)
原标题:django: Passing arguments to a custom view class (inheritance edition)
  • 时间:2012-05-23 14:34:05
  •  标签:
  • django

再说一遍,抱歉 这个问题可能很简单!

好吧,所以!

我的问题是我有一个保存模式函数, 它保存模型。 如果模型完全好( 是_ valid), 它会保存模型, 并重定向到页面A

如果模型不好, 或者请求是一Get, 那么我想重定向到页面B

但我做了好几次,真烦人! 我不想一直剪和粘糊,所以我想到了这个:

class SaveModel(View):
    def as_view(self):
        if request.method == "POST":
            form = SaveModel.getPostForm(self.request)
            if form.is_valid():
                processedForm = SaveModel.processForm(self.request)
                processedForm.save()
                if (self.success_template):
                    return render_to_response(self.success_template)
                else:
                    return render_to_response( pageA.html )
        else:
            form = SaveModel.getForm()
        if (self.context_object_name):
            contextName = context_object_name
        else:
            contextName =  form 
        if (self.template_name):
            return render_to_response(template_name,{contextName:form})
        else :
            return render_to_response( pageB.html ,{contextName:form})

    def getForm(self):
        return None

    def getPostForm(self,request):
        return None

    def processForm(self,form,request):
        return None

我定义了处理特定模型的其他类别, 比如,例如:

class StoryModelView(SaveModel):
    def getForm(self,request):
        return StoryForm()

    def getPostForm(self,request):
        return StoryForm(request.POST)

    def processForm(self,form,request):
        theStory = form.save(commit=False)
        theStory.user = request.user
        return theStory

最后,在我的urls.py.i中,我将提到(如上所述)使用类似模式的模型:

url(r ^addStory/$ ,
    StoryModelView.as_view(
        context_object_name= form ,
        template_name= accounts/addStory.html ,
        success_template= accounts/addStorySuccess.html 
    )
),

虽然这似乎行不通, 但Pycharm向我保证, 我对 self. context_object_ name 等的引用是无效的 。 我对 Python 和 Django ( 所以我认为我应该和他们一起建立一个网站! 聪明的Andrew! ), 所以我确定我错过了一大堆事情( 抽象的方法和东西... python 这样做, 对不对? )

我要怎么做才能让这一切奏效?

最佳回答

回答我!

可能是因为我从未使用过它, 因为我最后还是坚持我的代码。

万一有人像我一样 换成Python和django 想要看看整个事情是如何运作的 我们到了!

class SaveModel(View):
    success_template = None
    context_object_name = None
    template_name = None

    def post(self, request):
        form = self.getPostForm(self.request)
        if form.is_valid():
            processedForm = self.processForm(form,self.request)
            processedForm.save()
            if self.success_template:
                return render_to_response(self.success_template)
            else:
                return render_to_response( accounts/addStorySuccess.html )
        else:
            self.renderValidations(form)

    def get(self,request):
        form = self.getForm()
        self.renderValidations(form)

    def renderValidations(self,form):
        if self.context_object_name:
            contextName = self.context_object_name
        else:
            contextName =  form 
        if self.template_name:
            return render_to_response(self.template_name,{contextName:form})
        else :
            return render_to_response( accounts/addStory.html ,{contextName:form})

    def getForm(self):
        return None

    def getPostForm(self,request):
        return None

    def processForm(self,form,request):
        return None    

这是主类,然后我可以 取代它喜欢这样:

class StoryModelView(SaveModel):
    def getForm(self):
        return StoryForm()

    def getPostForm(self,request):
        return StoryForm(request.POST)

    def processForm(self,form,request):
        theStory = form.save(commit=False)
        theStory.user = request.user
        return theStory

我把自己绊倒了几次。 它似乎神奇地被发送到 Python 中, 并有所有的方法调用, 但你需要它作为方法声明的第一个参数( 但你在拨打/使用方法时, 不需要使用它) 。

我对电话的“程序”或命令是什么没有很好的认识, 调度被指为要推翻的东西, 但我怀疑这仅仅是在需要改变/ 处理不同请求类型( GET、 POST、 HEAD 等) 时/ 处理时/ 时/ 时( GET、 POST、 HEAD 等) 需要改变的地方。

哦!!!

url(r ^addStory/$ ,
    StoryModelView.as_view(
        context_object_name= form ,
        template_name =  accounts/addStory.html ,
        success_template=  accounts/addStorySuccess.html 
    )
),

只要这些参数在压倒一切的等级里 被定义就好

我的班级都工作,女人都想要我 使用我的代码,这也会发生在你身上!

* 结果是非典型的和虚构的。 你的结果可能会不同。

问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签