django : form_valid() missing 1 required positional argument: 'form'












0















I'm wondering how I can solve this issue in my form_valid() function :




form_valid() missing 1 required positional argument: 'form'




I'm using Django Class Based View and formsets. But, it could be possible to get IntegrityError and I added a try/except in order to save the formset when forms are valid, and redirect the template with an error message when I get this issue.



class AnimalCreateView(CreateView):
model = Animal
template_name = 'animal_form.html'

def get_context_data(self, **kwargs):
context = super(AnimalCreateView, self).get_context_data(**kwargs)
foo_queryset = Foo.objects.all()
context['FooFormSets'] = FooFormSet(self.request.POST or None, self.request.FILES or None,
prefix='foo', queryset=foo_queryset)
return context

def form_valid(self, request, form):
context = self.get_context_data()
formsets = context['FooFormSets']
if form.is_valid():
self.object = form.save()
try:
if formsets.is_valid():
formsets.instance = self.object
formsets.save(commit=False)
for element in formsets:
element.save(commit=False)
formsets.save()
except IntegrityError:
messages.error(self.request, _(f"Issue with foo"))
return render(request, self.template_name)

return super(AnimalCreateView, self).form_valid(form)


I would like to know what I have to do in my form_valid() function in order to solve my issue and redirect user on the same template form with error message.



Thank you










share|improve this question


















  • 2





    def form_valid(self, request, form): should be def form_valid(self, form):.

    – bdoubleu
    Nov 22 '18 at 10:55
















0















I'm wondering how I can solve this issue in my form_valid() function :




form_valid() missing 1 required positional argument: 'form'




I'm using Django Class Based View and formsets. But, it could be possible to get IntegrityError and I added a try/except in order to save the formset when forms are valid, and redirect the template with an error message when I get this issue.



class AnimalCreateView(CreateView):
model = Animal
template_name = 'animal_form.html'

def get_context_data(self, **kwargs):
context = super(AnimalCreateView, self).get_context_data(**kwargs)
foo_queryset = Foo.objects.all()
context['FooFormSets'] = FooFormSet(self.request.POST or None, self.request.FILES or None,
prefix='foo', queryset=foo_queryset)
return context

def form_valid(self, request, form):
context = self.get_context_data()
formsets = context['FooFormSets']
if form.is_valid():
self.object = form.save()
try:
if formsets.is_valid():
formsets.instance = self.object
formsets.save(commit=False)
for element in formsets:
element.save(commit=False)
formsets.save()
except IntegrityError:
messages.error(self.request, _(f"Issue with foo"))
return render(request, self.template_name)

return super(AnimalCreateView, self).form_valid(form)


I would like to know what I have to do in my form_valid() function in order to solve my issue and redirect user on the same template form with error message.



Thank you










share|improve this question


















  • 2





    def form_valid(self, request, form): should be def form_valid(self, form):.

    – bdoubleu
    Nov 22 '18 at 10:55














0












0








0








I'm wondering how I can solve this issue in my form_valid() function :




form_valid() missing 1 required positional argument: 'form'




I'm using Django Class Based View and formsets. But, it could be possible to get IntegrityError and I added a try/except in order to save the formset when forms are valid, and redirect the template with an error message when I get this issue.



class AnimalCreateView(CreateView):
model = Animal
template_name = 'animal_form.html'

def get_context_data(self, **kwargs):
context = super(AnimalCreateView, self).get_context_data(**kwargs)
foo_queryset = Foo.objects.all()
context['FooFormSets'] = FooFormSet(self.request.POST or None, self.request.FILES or None,
prefix='foo', queryset=foo_queryset)
return context

def form_valid(self, request, form):
context = self.get_context_data()
formsets = context['FooFormSets']
if form.is_valid():
self.object = form.save()
try:
if formsets.is_valid():
formsets.instance = self.object
formsets.save(commit=False)
for element in formsets:
element.save(commit=False)
formsets.save()
except IntegrityError:
messages.error(self.request, _(f"Issue with foo"))
return render(request, self.template_name)

return super(AnimalCreateView, self).form_valid(form)


I would like to know what I have to do in my form_valid() function in order to solve my issue and redirect user on the same template form with error message.



Thank you










share|improve this question














I'm wondering how I can solve this issue in my form_valid() function :




form_valid() missing 1 required positional argument: 'form'




I'm using Django Class Based View and formsets. But, it could be possible to get IntegrityError and I added a try/except in order to save the formset when forms are valid, and redirect the template with an error message when I get this issue.



class AnimalCreateView(CreateView):
model = Animal
template_name = 'animal_form.html'

def get_context_data(self, **kwargs):
context = super(AnimalCreateView, self).get_context_data(**kwargs)
foo_queryset = Foo.objects.all()
context['FooFormSets'] = FooFormSet(self.request.POST or None, self.request.FILES or None,
prefix='foo', queryset=foo_queryset)
return context

def form_valid(self, request, form):
context = self.get_context_data()
formsets = context['FooFormSets']
if form.is_valid():
self.object = form.save()
try:
if formsets.is_valid():
formsets.instance = self.object
formsets.save(commit=False)
for element in formsets:
element.save(commit=False)
formsets.save()
except IntegrityError:
messages.error(self.request, _(f"Issue with foo"))
return render(request, self.template_name)

return super(AnimalCreateView, self).form_valid(form)


I would like to know what I have to do in my form_valid() function in order to solve my issue and redirect user on the same template form with error message.



Thank you







django django-templates django-views






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 10:54









ChocoBombChocoBomb

718




718








  • 2





    def form_valid(self, request, form): should be def form_valid(self, form):.

    – bdoubleu
    Nov 22 '18 at 10:55














  • 2





    def form_valid(self, request, form): should be def form_valid(self, form):.

    – bdoubleu
    Nov 22 '18 at 10:55








2




2





def form_valid(self, request, form): should be def form_valid(self, form):.

– bdoubleu
Nov 22 '18 at 10:55





def form_valid(self, request, form): should be def form_valid(self, form):.

– bdoubleu
Nov 22 '18 at 10:55












1 Answer
1






active

oldest

votes


















4














def form_valid(self, request, form): should be def form_valid(self, form):.



You can access request object through self.request.



def form_valid(self, form):
context = self.get_context_data()
formsets = context['FooFormSets']
if form.is_valid():
self.object = form.save()
try:
if formsets.is_valid():
formsets.instance = self.object
formsets.save(commit=False)
for element in formsets:
element.save(commit=False)
formsets.save()
except IntegrityError:
messages.error(self.request, _(f"Issue with foo"))
return render(self.request, self.template_name)


Additionally, you don't need to call if form.is_valid(): a second time as that's already been determined - hence executing form_valid.






share|improve this answer


























  • Yes you're right ! I just have one more question, Do I have to replace except IntegrityError or it's good ? Because formsets.save() is the direct cause to the folowwing exception.

    – ChocoBomb
    Nov 22 '18 at 11:00













  • If you expect an IntegrityError then it doesn't hurt but without more info on your models I have no idea. For example a duplicate key could cause an error if your foreign key is a OneToOneField

    – bdoubleu
    Nov 22 '18 at 11:04











  • I will create a new question according to my IntegrityError thank you !

    – ChocoBomb
    Nov 22 '18 at 13:51











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429359%2fdjango-form-valid-missing-1-required-positional-argument-form%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














def form_valid(self, request, form): should be def form_valid(self, form):.



You can access request object through self.request.



def form_valid(self, form):
context = self.get_context_data()
formsets = context['FooFormSets']
if form.is_valid():
self.object = form.save()
try:
if formsets.is_valid():
formsets.instance = self.object
formsets.save(commit=False)
for element in formsets:
element.save(commit=False)
formsets.save()
except IntegrityError:
messages.error(self.request, _(f"Issue with foo"))
return render(self.request, self.template_name)


Additionally, you don't need to call if form.is_valid(): a second time as that's already been determined - hence executing form_valid.






share|improve this answer


























  • Yes you're right ! I just have one more question, Do I have to replace except IntegrityError or it's good ? Because formsets.save() is the direct cause to the folowwing exception.

    – ChocoBomb
    Nov 22 '18 at 11:00













  • If you expect an IntegrityError then it doesn't hurt but without more info on your models I have no idea. For example a duplicate key could cause an error if your foreign key is a OneToOneField

    – bdoubleu
    Nov 22 '18 at 11:04











  • I will create a new question according to my IntegrityError thank you !

    – ChocoBomb
    Nov 22 '18 at 13:51
















4














def form_valid(self, request, form): should be def form_valid(self, form):.



You can access request object through self.request.



def form_valid(self, form):
context = self.get_context_data()
formsets = context['FooFormSets']
if form.is_valid():
self.object = form.save()
try:
if formsets.is_valid():
formsets.instance = self.object
formsets.save(commit=False)
for element in formsets:
element.save(commit=False)
formsets.save()
except IntegrityError:
messages.error(self.request, _(f"Issue with foo"))
return render(self.request, self.template_name)


Additionally, you don't need to call if form.is_valid(): a second time as that's already been determined - hence executing form_valid.






share|improve this answer


























  • Yes you're right ! I just have one more question, Do I have to replace except IntegrityError or it's good ? Because formsets.save() is the direct cause to the folowwing exception.

    – ChocoBomb
    Nov 22 '18 at 11:00













  • If you expect an IntegrityError then it doesn't hurt but without more info on your models I have no idea. For example a duplicate key could cause an error if your foreign key is a OneToOneField

    – bdoubleu
    Nov 22 '18 at 11:04











  • I will create a new question according to my IntegrityError thank you !

    – ChocoBomb
    Nov 22 '18 at 13:51














4












4








4







def form_valid(self, request, form): should be def form_valid(self, form):.



You can access request object through self.request.



def form_valid(self, form):
context = self.get_context_data()
formsets = context['FooFormSets']
if form.is_valid():
self.object = form.save()
try:
if formsets.is_valid():
formsets.instance = self.object
formsets.save(commit=False)
for element in formsets:
element.save(commit=False)
formsets.save()
except IntegrityError:
messages.error(self.request, _(f"Issue with foo"))
return render(self.request, self.template_name)


Additionally, you don't need to call if form.is_valid(): a second time as that's already been determined - hence executing form_valid.






share|improve this answer















def form_valid(self, request, form): should be def form_valid(self, form):.



You can access request object through self.request.



def form_valid(self, form):
context = self.get_context_data()
formsets = context['FooFormSets']
if form.is_valid():
self.object = form.save()
try:
if formsets.is_valid():
formsets.instance = self.object
formsets.save(commit=False)
for element in formsets:
element.save(commit=False)
formsets.save()
except IntegrityError:
messages.error(self.request, _(f"Issue with foo"))
return render(self.request, self.template_name)


Additionally, you don't need to call if form.is_valid(): a second time as that's already been determined - hence executing form_valid.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 11:01

























answered Nov 22 '18 at 10:57









bdoubleubdoubleu

632112




632112













  • Yes you're right ! I just have one more question, Do I have to replace except IntegrityError or it's good ? Because formsets.save() is the direct cause to the folowwing exception.

    – ChocoBomb
    Nov 22 '18 at 11:00













  • If you expect an IntegrityError then it doesn't hurt but without more info on your models I have no idea. For example a duplicate key could cause an error if your foreign key is a OneToOneField

    – bdoubleu
    Nov 22 '18 at 11:04











  • I will create a new question according to my IntegrityError thank you !

    – ChocoBomb
    Nov 22 '18 at 13:51



















  • Yes you're right ! I just have one more question, Do I have to replace except IntegrityError or it's good ? Because formsets.save() is the direct cause to the folowwing exception.

    – ChocoBomb
    Nov 22 '18 at 11:00













  • If you expect an IntegrityError then it doesn't hurt but without more info on your models I have no idea. For example a duplicate key could cause an error if your foreign key is a OneToOneField

    – bdoubleu
    Nov 22 '18 at 11:04











  • I will create a new question according to my IntegrityError thank you !

    – ChocoBomb
    Nov 22 '18 at 13:51

















Yes you're right ! I just have one more question, Do I have to replace except IntegrityError or it's good ? Because formsets.save() is the direct cause to the folowwing exception.

– ChocoBomb
Nov 22 '18 at 11:00







Yes you're right ! I just have one more question, Do I have to replace except IntegrityError or it's good ? Because formsets.save() is the direct cause to the folowwing exception.

– ChocoBomb
Nov 22 '18 at 11:00















If you expect an IntegrityError then it doesn't hurt but without more info on your models I have no idea. For example a duplicate key could cause an error if your foreign key is a OneToOneField

– bdoubleu
Nov 22 '18 at 11:04





If you expect an IntegrityError then it doesn't hurt but without more info on your models I have no idea. For example a duplicate key could cause an error if your foreign key is a OneToOneField

– bdoubleu
Nov 22 '18 at 11:04













I will create a new question according to my IntegrityError thank you !

– ChocoBomb
Nov 22 '18 at 13:51





I will create a new question according to my IntegrityError thank you !

– ChocoBomb
Nov 22 '18 at 13:51




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429359%2fdjango-form-valid-missing-1-required-positional-argument-form%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()