Created
July 28, 2018 00:15
-
-
Save renjithsraj/1f6a1f65292053704ad2c9be5def36aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def recipe_form(request): | |
if not request.user.is_superuser: | |
raise Http404 | |
ImageFormSet = modelformset_factory(Recipe_images, form=Recipe_imageForm, extra=10) | |
if request.method == 'POST': | |
form = Recipe_form(request.POST or None, request.FILES or None) | |
formset = ImageFormSet(None, None, queryset=Recipe_images.objects.none()) | |
if form.is_valid() and formset.is_valid(): | |
instance = form.save(commit=False) | |
instance.save() | |
for image_form in formset.cleaned_datas: | |
image = image_form['image'] | |
photo = Recipe_images(post=form, image=image) | |
photo.save() | |
messages.success(request, "Successfully created") | |
return HttpResponseRedirect(instance.get_absolute_url()) | |
else: | |
messages.error(request, "Not successfully created") | |
else: | |
form = Recipe_form() | |
formset = ImageFormSet(queryset=Recipe_images.objects.none()) | |
context ={ | |
"form": form, | |
"formset": formset | |
} | |
return render(request, 'personalsite/Recipe_form.html', context) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment