python - 500 internal server error while uploading images django -
i have form divided 2 parts. mean form has rental information fields , multiple image upload.i have used reactjs in frontend , django in backend.rentals information saved database images not.it throws 500 internal server error.i have app deployed on digital ocean(nginx, gunicorn used).link app commonrentspace.me
here code
addrent.js(upload image code focused more shortening code line)
let image = []; class renderphotos extends react.component { constructor(props, context) { super(props, context); this.state = { files: [] }; } ondrop(files) { console.log('received files: ', files); this.setstate({ files: files }); function getcookie(name) { var cookievalue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); (var = 0; < cookies.length; i++) { var cookie = jquery.trim(cookies[i]); // cookie string begin name want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookievalue = decodeuricomponent(cookie.substring(name.length + 1)); break; } } } return cookievalue; } var csrftoken = getcookie('csrftoken'); function csrfsafemethod(method) { return (/^(get|head|options|trace)$/.test(method)); } $.ajaxsetup({ beforesend: function(xhr, settings) { if (!csrfsafemethod(settings.type) && !this.crossdomain) { xhr.setrequestheader("x-csrftoken", csrftoken); } } }); image = new formdata(files); $.each(files,function(i,file){ image.append('image',file); }); } showfiles() { const { files } = this.state; console.log('files',files); if (!files.length) { return null; } return ( <div> <h3>dropped files: </h3> <ul classname="gallery"> { _.map(files,(file, idx) => { return ( <div classname="col-md-3"> <li key={idx}> <img src={file.preview} classname="img-fluid" width={200}/> <div classname="imagename">{file.name}</div> </li> </div> ) }) } </ul> </div> ); } render() { return ( <div> <h3 classname="formheadingh3">photos can bring space life</h3> <p>add photos of spaces give more insight of space </p> <hr/> <div classname="col-md-12"> <form method="post" enctype="multipart/form-data"> <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}"/> <dropzone ondrop={this.ondrop.bind(this)} style={style} activestyle={activestyle} accept="image/*"> try dropping files here, or click select files upload. </dropzone> </form> {this.showfiles()} </div> <div classname="row continuebtn text-right"> <button classname="btn how-it-works pull-left" onclick={this.props.previousstep.bind(this)}><i classname="fa fa-hand-o-left"></i></button> <button classname="btn how-it-works pull-right" onclick={this.nextstep.bind(this)}>submit</button> </div> </div> ); } nextstep(step){ var senddata={'ownername':this.props.fieldvalues.ownername, 'email':this.props.fieldvalues.email, 'phonenumber':this.props.fieldvalues.phonenumber, 'listingname':this.props.fieldvalues.listingname, 'summary':this.props.fieldvalues.summary, 'property':this.props.fieldvalues.property, 'room':this.props.fieldvalues.room, 'price':this.props.fieldvalues.price, 'city':this.props.fieldvalues.city, 'place':this.props.fieldvalues.place, 'water':this.props.fieldvalues.water, 'amenities':this.props.fieldvalues.amenities.join(', ') } $.ajax({ url:"/add/space/", data:senddata, type:'post', success: function(data, textstatus, xhr ) { var pk = xhr.getresponseheader('pk-user'); console.log('pk is',pk); $.ajax({ url:"/upload/image/"+pk+"/", data:image, contenttype:false, processdata:false, type:'post', mimetype: "multipart/form-data", success: function(data) { console.log('success'); } }); window.location.href="http://commonrentpspace.me/"; // if use redirect, images not upload } }); } }
views.py
class addview(templateview): template_name = 'rentals/add.html' class addspaceview(view): def post(self,request,*args,**kwargs): print ('add space view',request) if request.post: response = httpresponse('') print('owner name is',request.post.get('ownername')) print('amenities',request.post.get('amenities')) rental = rental() rental.ownername = request.post.get('ownername') rental.email = request.post.get('email') rental.phonenumber = request.post.get('phonenumber') rental.listingname = request.post.get('listingname') rental.summary = request.post.get('summary') rental.property = request.post.get('property') rental.room = request.post.get('room') rental.price = request.post.get('price') rental.city = request.post.get('city') rental.place = request.post.get('place') rental.water = request.post.get('water') rental.amenities = request.post.get('amenities') rental.save() response['pk-user'] = rental.pk return response return httpresponse('rental information added') class uploadimage(view): model = rental template_name = 'rentals/add.html' print "hello" def get(self, request, *args, **kwargs): return render(request, self.template_name) def post(self,request,*args,**kwargs): try: rental = rental.objects.get(pk = self.kwargs['pk']) except rental.doesnotexist: error_dict = {'message': 'rental spae not found'} print "error rental not exist" return self.render(request,'rentals/add.html',error_dict) if request.files: file in request.files.getlist('image'): print('file',file) image = gallery.objects.create(rental = rental, image=file) print('image',image) print "uploading image" return httpresponse("uploaded successfully")
nginx configuration
upstream app_server { server 127.0.0.1:9000 fail_timeout=0; } server { listen 80 default_server; root /usr/share/nginx/html; index index.html index.htm; client_max_body_size 4g; server_name _; keepalive_timeout 5; gzip on; gzip_http_version 1.0; gzip_comp_level 2; gzip_proxied any; gzip_min_length 1100; gzip_buffers 16 8k; gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; # version of ie 6 don't handle compression on mime-types, disable them gzip_disable "msie [1-6].(?!.*sv1)"; # set vary header downstream proxies don't send cached gzipped content ie6 gzip_vary on; # django project's media files - amend required location /media { alias /home/django/django_project/media/; } # django project's static files - amend required location /static { alias /home/django/django_project/static; } # proxy static assests django admin panel location /static/admin { alias /home/django/django_project/static/admin/; } location / { proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header host $http_host; proxy_redirect off; proxy_pass http://app_server; } } upstream app_server { server 127.0.0.1:9000 fail_timeout=0; } server { listen 80 default_server; root /usr/share/nginx/html; index index.html index.htm; client_max_body_size 4g; server_name _; keepalive_timeout 5; gzip on; gzip_http_version 1.0; gzip_comp_level 2; gzip_proxied any; gzip_min_length 1100; gzip_buffers 16 8k; gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; # version of ie 6 don't handle compression on mime-types, disable them gzip_disable "msie [1-6].(?!.*sv1)"; # set vary header downstream proxies don't send cached gzipped content ie6 gzip_vary on; # django project's media files - amend required location /media { alias /home/django/django_project/media/; } # django project's static files - amend required location /static { alias /home/django/django_project/static; } # proxy static assests django admin panel location /static/admin { alias /home/django/django_project/static/admin/; } location / { proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header host $http_host; proxy_redirect off; proxy_pass http://app_server; } }
do need provide other information? might cause?
Comments
Post a Comment