Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to put this code into form to more explain i tried but not have results. #1

Open
LeandriT opened this issue Sep 23, 2016 · 0 comments

Comments

@LeandriT
Copy link

class PictureCreateView(CreateView):
model = Media
fields = "all"

def form_valid(self, form):
    self.object = form.save()
    files = [serialize(self.object)]
    data = {'files': files}
    print self.request
    response = JSONResponse(data, mimetype=response_mimetype(self.request))
    response['Content-Disposition'] = 'inline; filename=files.json'
    return response

def form_invalid(self, form):
    data = json.dumps(form.errors)
    return HttpResponse(content=data, status=400, content_type='application/json')

sismilar to

def PictureCreateView(request):
if request.is_ajax() or request.method=='POST':
frm_media = FormMedia(data=request.POST or None, )
if frm_media.is_valid():
object = frm_media.save()
files = [serialize(object)]
data = {'files': files}
response = JSONResponse(data, mimetype=response_mimetype(request))
response['Content-Disposition'] = 'inline; filename=files.json'
return HttpResponse(response)
else:
data = json.dumps(frm_media.errors)
return HttpResponse(content=data, status=400, content_type='application/json')
else:
frm_media = FormMedia()
return

in forms.py

class FormMedia(ModelForm):
model = Media
fields = "all"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant