Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:41:39+00:00 2026-06-04T12:41:39+00:00

again, apologies for what is probably a straightforward question! Ok, so! my problem is

  • 0

again, apologies for what is probably a straightforward question!

Ok, so!

my problem is i have a saveModel function, where it saves a model. If the model is all good (is_valid), it will save the model and redirect to pageA

if the model is bad, or the request is a GET, then i’d like to redirect to pageB

all well and good, but i do this several times, how annoying! I don’t want to cut and paste all the time, so i came up with this:

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

THEN, i define other classes to handle particular models, like, for example, so:

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

and then, finally, in my urls.py i will refer to (as above) the model to use like so:

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

This doesn’t seem to work though – pycharm assures me that my references to self.context_object_name and so on are invalid. I’m v. new to python and django (which is why i thought i’d build a website with them! clever andrew!), so i am sure that i’ve missed a whole bunch of things (abstract methods and stuff… python does that, right?)

what do i need to do to get this all working? Is this how i should be doing things?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:41:41+00:00Added an answer on June 4, 2026 at 12:41 pm

    ANSWER BY ME!

    Ok, so the comments everyone has written about the CreateView are probably correct. “Probably” because i never ended up using it, because i ended up sticking with my code instead.

    In case anybody is, like me, new to python and django and wants to see how the whole thing works, here we are!

    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    
    

    and that is the main class, then i can override it like so:

    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
    

    i tripped myself up with how “self” works in python a few times. it seems to be magically sent across with all method calls, but you need it as the first arg in the method declaration (but you never need to use it when calling/using the method)

    i think there’s only post or get for methods when overriding the View class. i don’t have a good idea of the “process” of the call, or what the order is, dispatch was mentioned as something to override, but i suspect that is only where i need to change when/how to deal with differing request types (GET, POST, HEAD etc)

    oh! the urls.py!

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

    i can just chuck whatever i want into that “as_view” call, and then, as long as those parameters are defined in the overriding class it’s all good.

    so yay! my classes all work and women want me. use my code, and this can happen to you too!*

    *results atypical and fictional. your results may differ.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Again I have a question regarding this plugin: http://t.wits.sg/2008/06/20/jquery-progress-bar-11/ What I want to achieve
Once again a question about the garbage collector in actionscript-3: If I have a
My apologies once again for asking another very junior question. For one reason or
Apologies for asking more or less the same question again. I just noticed that
Apologies for posting yet another Java jar question. I did have a look at
Apologies, but I have searched and read previous answers on this question but couldn't
Apologies for asking this question again. I thought I found the infomation I was
Me again with a dumb question/scenario I need advice on. I have the following
Again a SED question from me :) So, same as last time, I'm wrestling
Hi Again Masters Of The Web :) Now, I have got a new stupid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.