Update value in database
Hi! I have a real estate django app and i have a form to users insert a new property. I also have in the form several images fields that users can use to upload images to database whitout problem. The problem is if the *user want to delete one of the images field.* I have an ajax request that sends the id of the property and the field name to update (delete) on database. function delete_img(i){ var prim_key = document.getElementById('list_id').value; var id_number = i.id; $.ajax({ type: 'GET', url: '', data: {'csrfmiddlewaretoken' : "{{ csrf_token }}", 'img_id':id_number , 'post_id':prim_key }, success: function(data){ if(data) { console.log(id_number + " " + prim_key); document.getElementById('img_' + id_number).src = "http://localhost:8000/media/upload_img_default.png";; } } }); In views.py i have a function that receives the data from ajax and executes the task. @csrf_exempt def delete_img(request): if request.method=='GET' and request.is_ajax(): img_id = request.GET['img_id'] post_id = request.GET['post_id'] try: obj = Listing.objects.get(id=post_id) obj1 = getattr(obj,img_id) obj1.value = None obj.save() return True except Listing.DoesNotExist: return False else: return False Can anyone help me with this situation? Thanks for your help. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9cb23263-95e4-4060-b855-09a0e30cb89d%40googlegroups.com.
Re: ElementTree problem saving images
Hi, thanks for your help. i solved the situation like this: img = props.find('images') photos = [] for child in img[:len(img)]: photos.append(child[0].text) try: img_1_url = photos[1] except IndexError: img_1_url = None try: img_2_url = photos[2] except IndexError: img_2_url = None Just an example, but like this, for each property i could attached the correct number of images. Thanks again for all of you that tried to help me! Best regards quinta-feira, 24 de Outubro de 2019 às 16:28:00 UTC+1, Nuno Vieira escreveu: > > Hi, > > i receive an xml file and i am parsing the values and sving them to > database. > >> XML Example: > > ... > ... > ... > > 9896 > 2019-07-11 13:12:09 > BC30401 > 895000 > EUR > sale > 0 > 0 > 0 > > 915 > > Villa > > 37.134720 > -8.020436 > > optional location detail > Vilamoura > Algarve > Portugal > 4 > 3 > 1 > > > > https://bc-villas.com/wp-content/uploads/2019/07/IMG_7064.jpg > > > > > https://bc-villas.com/wp-content/uploads/2019/07/IMG_7065.jpg > > > > > https://bc-villas.com/wp-content/uploads/2019/07/IMG_7066.jpg > > > > > https://bc-villas.com/wp-content/uploads/2019/07/IMG_7067.jpg > > > > > https://bc-villas.com/wp-content/uploads/2019/07/IMG_7068.jpg > > > > > https://bc-villas.com/wp-content/uploads/2019/07/IMG_7069.jpg > > > > > https://bc-villas.com/wp-content/uploads/2019/07/IMG_7071.jpg > > > > > https://bc-villas.com/wp-content/uploads/2019/07/IMG_7072.jpg > > > > > https://bc-villas.com/wp-content/uploads/2019/07/IMG_7073.jpg > > > > > https://bc-villas.com/wp-content/uploads/2019/07/IMG_7075.jpg > > > > > https://bc-villas.com/wp-content/uploads/2019/07/IMG_7077.jpg > > >> > > > It´s all working ok but with images its a litle confused because i can > only save the first and the last image. If a property have 12 pictures i > can only save the picture nr 1 and picture nr 12, if the property have 8 > pictures i can only save picture nr1 and picture nr 8. It´s weird! > > My code to get the pictures url: > img = props.find('images') > > for child in img: > > if child.get('id') == '1': >img_main_url = child.find('url' > ).text > >if child.get('id') == '2': > >img_1_url = child.find('url').text >else: >img_1_url = None >if child.get('id') == '3': >img_2_url = child.find('url').text >else: >img_2_url = None >if child.get('id') == '4': >img_3_url = child.find('url').text >else: >img_3_url = None >if child.get('id') == '5': >img_4_url = child.find('url').text >else: >img_4_url = None > > > I could use a litle help from someone more experienced in django. > > Thanks for your help > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/36560a44-3601-4330-9412-c1bc5d19a7a8%40googlegroups.com.
Re: ElementTree problem saving images
Thanks for your feedback. The problem is if i set all the urls to None before, and a property have only 6 images of 20, on database saves the 6 images of this property and saves also the other image columns with images from a different property, but when i use the else blocks instead of setting all to None before the loop, it only saves the first (nr 1) and the last image (nr 6) of the property. I have no idea why this behavior. I really need some help. Thanks. sexta-feira, 25 de Outubro de 2019 às 01:01:45 UTC+1, Arthur Antoine escreveu: > > The image urls are being set to None on every iteration where the id > doesn't match. Try setting all of the urls to None before the loop, then > remove the else blocks. > > On Thursday, October 24, 2019 at 11:28:00 AM UTC-4, Nuno Vieira wrote: >> >> Hi, >> >> i receive an xml file and i am parsing the values and sving them to >> database. >> >>> XML Example: >> >> ... >> ... >> ... >> >> 9896 >> 2019-07-11 13:12:09 >> BC30401 >> 895000 >> EUR >> sale >> 0 >> 0 >> 0 >> >> 915 >> >> Villa >> >> 37.134720 >> -8.020436 >> >> optional location detail >> Vilamoura >> Algarve >> Portugal >> 4 >> 3 >> 1 >> >> >> >> https://bc-villas.com/wp-content/uploads/2019/07/IMG_7064.jpg >> >> >> >> >> https://bc-villas.com/wp-content/uploads/2019/07/IMG_7065.jpg >> >> >> >> >> https://bc-villas.com/wp-content/uploads/2019/07/IMG_7066.jpg >> >> >> >> >> https://bc-villas.com/wp-content/uploads/2019/07/IMG_7067.jpg >> >> >> >> >> https://bc-villas.com/wp-content/uploads/2019/07/IMG_7068.jpg >> >> >> >> >> https://bc-villas.com/wp-content/uploads/2019/07/IMG_7069.jpg >> >> >> >> >> https://bc-villas.com/wp-content/uploads/2019/07/IMG_7071.jpg >> >> >> >> >> https://bc-villas.com/wp-content/uploads/2019/07/IMG_7072.jpg >> >> >> >> >> https://bc-villas.com/wp-content/uploads/2019/07/IMG_7073.jpg >> >> >> >> >> https://bc-villas.com/wp-content/uploads/2019/07/IMG_7075.jpg >> >> >> >> >> https://bc-villas.com/wp-content/uploads/2019/07/IMG_7077.jpg >> >> >>> >> >> >> It´s all working ok but with images its a litle confused because i can >> only save the first and the last image. If a property have 12 pictures i >> can only save the picture nr 1 and picture nr 12, if the property have 8 >> pictures i can only save picture nr1 and picture nr 8. It´s weird! >> >> My code to get the pictures url: >> img = props.find('images') >> >> for child in img: >> >> if child.get('id') == '1': >>img_main_url = child.find('url' >> ).text >> >>if child.get('id') == '2': >> >>img_1_url = child.find('url').text >>else: >>img_1_url = None >>if child.get('id') == '3': >>img_2_url = child.find('url').text >>else: >>img_2_url = None >>if child.get('id') == '4': >>img_3_url = child.find('url').text >>else: >>img_3_url = None >>if child.get('id') == '5': >>img_4_url = child.find('url').text >>else: >>img_4_url = None >> >> >> I could use a litle help from someone more experienced in django. >> >> Thanks for your help >> >> -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/249cb5ad-668e-4fe9-aa54-f55c9e3a8ae6%40googlegroups.com.
ElementTree problem saving images
Hi, i receive an xml file and i am parsing the values and sving them to database. > XML Example: ... ... ... 9896 2019-07-11 13:12:09 BC30401 895000 EUR sale 0 0 0 915 Villa 37.134720 -8.020436 optional location detail Vilamoura Algarve Portugal 4 3 1 https://bc-villas.com/wp-content/uploads/2019/07/IMG_7064.jpg https://bc-villas.com/wp-content/uploads/2019/07/IMG_7065.jpg https://bc-villas.com/wp-content/uploads/2019/07/IMG_7066.jpg https://bc-villas.com/wp-content/uploads/2019/07/IMG_7067.jpg https://bc-villas.com/wp-content/uploads/2019/07/IMG_7068.jpg https://bc-villas.com/wp-content/uploads/2019/07/IMG_7069.jpg https://bc-villas.com/wp-content/uploads/2019/07/IMG_7071.jpg https://bc-villas.com/wp-content/uploads/2019/07/IMG_7072.jpg https://bc-villas.com/wp-content/uploads/2019/07/IMG_7073.jpg https://bc-villas.com/wp-content/uploads/2019/07/IMG_7075.jpg https://bc-villas.com/wp-content/uploads/2019/07/IMG_7077.jpg > It´s all working ok but with images its a litle confused because i can only save the first and the last image. If a property have 12 pictures i can only save the picture nr 1 and picture nr 12, if the property have 8 pictures i can only save picture nr1 and picture nr 8. It´s weird! My code to get the pictures url: img = props.find('images') for child in img: if child.get('id') == '1': img_main_url = child.find('url').text if child.get('id') == '2': img_1_url = child.find('url').text else: img_1_url = None if child.get('id') == '3': img_2_url = child.find('url').text else: img_2_url = None if child.get('id') == '4': img_3_url = child.find('url').text else: img_3_url = None if child.get('id') == '5': img_4_url = child.find('url').text else: img_4_url = None I could use a litle help from someone more experienced in django. Thanks for your help -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9605acae-a45c-4ac2-b182-ef7f20659db2%40googlegroups.com.
Re: delete image url from database
I am saving the image to media folder and save the url in database. photo_5 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True) Then when the user clicks in trash can icon, i have an ajax request to delete the image from database. But there is the problem, i am not able to update the url in database or to delete. I already tried with update Listing.objects.all().filter(id = 1511).update(photo_5 = None) It seems that i can not change the value of photo_5. quarta-feira, 16 de Outubro de 2019 às 15:23:07 UTC+1, Nuno Vieira escreveu: > > Hi there, > > i am creating a real estate app and i want users to insert and delete > images for the houses. > > It's all working great except the delete. > > In my views.py i have the code: > > listing = Listing.objects.get(id=1511) > if listing: > listing.photo_5 = None > listing.save(['photo_5']) > return True > > photo_5 is an Imagefield and i want to change the value of that field in > database to None. > > Why is not changing? > > Thanks for your help. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/70f0da13-24dd-4670-9fbf-e9cdf46bed34%40googlegroups.com.
delete image url from database
Hi there, i am creating a real estate app and i want users to insert and delete images for the houses. It's all working great except the delete. In my views.py i have the code: listing = Listing.objects.get(id=1511) if listing: listing.photo_5 = None listing.save(['photo_5']) return True photo_5 is an Imagefield and i want to change the value of that field in database to None. Why is not changing? Thanks for your help. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f24fda94-428d-4f76-b632-4078d948fdcf%40googlegroups.com.
Re: parse xml from url to database
Hi Ewen, thanks for the reply. I have extended user model with a profile model and a new field (xml_ky). The goal is to verify each user profile and if they have an xml url on field xml_ky, than populate the database with the properties located on that url. Thanks for your help. quinta-feira, 13 de Junho de 2019 às 00:04:33 UTC+1, Ewen Le Bihan escreveu: > > Hi, where does prof.xml_ky comes from ? > > Le mercredi 12 juin 2019 18:22:01 UTC+2, Nuno Vieira a écrit : >> >> Hi there. >> >> I´m having some problems on parsing a real estate xml to database. >> The user profile have url field to import properties automatically from >> xml feed (url). The user can insert mannually or automatically, if the user >> insert the url feed then it should be able to get the objects and save them >> to mysql database automatically. >> The function is not working and i do not know why, gives no error but >> don´t populate the database. >> >> Models.py: >> class Listing(models.Model): >> '''Create table listing in db ''' >> realtor = models.ForeignKey(User, on_delete=models.CASCADE) >> title = models.CharField(max_length=200) >> address = models.CharField(max_length=200) >> city = models.CharField(max_length=100) >> state = models.CharField(max_length=100) >> zipcode = models.CharField(max_length=20) >> longitude = models.DecimalField(max_digits=9, decimal_places=6,default=0) >> latitude = models.DecimalField(max_digits=9, decimal_places=6, default=0) >> description = models.TextField(blank=True) >> price = models.IntegerField() >> bedrooms = models.IntegerField() >> bathrooms = models.DecimalField(max_digits=2, decimal_places=1) >> garage = models.IntegerField(default=0) >> sqmt = models.IntegerField(blank=True) >> lot_size = models.DecimalField(max_digits=5, decimal_places=1,blank=True) >> On_Processing = 'On Processing' >> AMais = 'A+' >> A = 'A' >> B = 'B' >> BMenos = 'B-' >> C = 'C' >> D = 'D' >> E = 'E' >> F = 'F' >> ENERGY_RATE_CHOICES = [ >> (On_Processing, 'On Processing'), >> (AMais, 'A+'), >> (A, 'A'), >> (B, 'B'), >> (BMenos, 'B-'), >> (C, 'C'), >> (D, 'D'), >> (E, 'E'), >> (F, 'F'), >> ] >> energy_rate = models.CharField( >> max_length=12, >> choices=ENERGY_RATE_CHOICES, >> default=On_Processing, >> ) >> photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/', default= >> 'default.jpg') >> photo_main_url = models.URLField(blank = True) >> is_published = models.BooleanField(default=True) >> list_date = models.DateTimeField(default=datetime.now, blank=True) >> video_url = models.CharField(max_length=100, blank=True) >> virtual_tour = models.URLField(blank=True) >> visit_num = models.PositiveIntegerField(default=0) >> >> def __str__(self): >> return self.title >> >> def get_absolute_url(self): >> return reverse('listing-detail', kwargs={'pk': self.pk}) >> >> Function on xml_parse.py: >> ... >> import xml.etree.ElementTree as ET >> import requests >> from users.models import Profile >> >> >> def xml_parse(self): >> prof_list = Profile.objects.all() >> for prof in prof_list: >> if prof.xml_ky is not None: >> url = xml_ky >> response = request.get(url) >> with open('xml/%s' % self.id + '/feed.xml', 'wb') as file: >> file.write(response.content) >> >> tree = ET.parse('xml/%s' % self.id + '/feed.xml') >> root = tree.getroot() >> for props in root.iter('property'): >> listing, created = Listing.object.create( >> title = props.find('type').text + ' ' + props.find('town').text, >> address = props.find('location_detail').text, >> city = props.find('town').text, >> state = props.find('province').text, >> zipcode = props.find('zipcode').text, >> loc = props.find('location'), >> longitude = loc.find('longitude').text, >> latitude = loc.find('latitude').text, >> desc = props.find('desc'), >> description = desc.find('en').text, >> price = props.find('price').text, >> bedrooms = props.find('beds').text, >> bathrooms = props.find('baths').text, &g
Re: parse xml from url to database
quarta-feira, 12 de Junho de 2019 às 17:22:01 UTC+1, Nuno Vieira escreveu: > > Hi there. > > I´m having some problems on parsing a real estate xml to database. > The user profile have url field to import properties automatically from > xml feed (url). The user can insert mannually or automatically, if the user > insert the url feed then it should be able to get the objects and save them > to mysql database automatically. > The function is not working and i do not know why, gives no error but > don´t populate the database. > > Models.py: > class Listing(models.Model): > '''Create table listing in db ''' > realtor = models.ForeignKey(User, on_delete=models.CASCADE) > title = models.CharField(max_length=200) > address = models.CharField(max_length=200) > city = models.CharField(max_length=100) > state = models.CharField(max_length=100) > zipcode = models.CharField(max_length=20) > longitude = models.DecimalField(max_digits=9, decimal_places=6,default=0) > latitude = models.DecimalField(max_digits=9, decimal_places=6, default=0) > description = models.TextField(blank=True) > price = models.IntegerField() > bedrooms = models.IntegerField() > bathrooms = models.DecimalField(max_digits=2, decimal_places=1) > garage = models.IntegerField(default=0) > sqmt = models.IntegerField(blank=True) > lot_size = models.DecimalField(max_digits=5, decimal_places=1,blank=True) > On_Processing = 'On Processing' > AMais = 'A+' > A = 'A' > B = 'B' > BMenos = 'B-' > C = 'C' > D = 'D' > E = 'E' > F = 'F' > ENERGY_RATE_CHOICES = [ > (On_Processing, 'On Processing'), > (AMais, 'A+'), > (A, 'A'), > (B, 'B'), > (BMenos, 'B-'), > (C, 'C'), > (D, 'D'), > (E, 'E'), > (F, 'F'), > ] > energy_rate = models.CharField( > max_length=12, > choices=ENERGY_RATE_CHOICES, > default=On_Processing, > ) > photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/', default= > 'default.jpg') > photo_main_url = models.URLField(blank = True) > is_published = models.BooleanField(default=True) > list_date = models.DateTimeField(default=datetime.now, blank=True) > video_url = models.CharField(max_length=100, blank=True) > virtual_tour = models.URLField(blank=True) > visit_num = models.PositiveIntegerField(default=0) > > def __str__(self): > return self.title > > def get_absolute_url(self): > return reverse('listing-detail', kwargs={'pk': self.pk}) > > Function on xml_parse.py: > ... > import xml.etree.ElementTree as ET > import requests > from users.models import Profile > > > def xml_parse(self): > prof_list = Profile.objects.all() > for prof in prof_list: > if prof.xml_ky is not None: > url = xml_ky > response = request.get(url) > with open('xml/%s' % self.id + '/feed.xml', 'wb') as file: > file.write(response.content) > > tree = ET.parse('xml/%s' % self.id + '/feed.xml') > root = tree.getroot() > for props in root.iter('property'): > listing, created = Listing.object.create( > title = props.find('type').text + ' ' + props.find('town').text, > address = props.find('location_detail').text, > city = props.find('town').text, > state = props.find('province').text, > zipcode = props.find('zipcode').text, > loc = props.find('location'), > longitude = loc.find('longitude').text, > latitude = loc.find('latitude').text, > desc = props.find('desc'), > description = desc.find('en').text, > price = props.find('price').text, > bedrooms = props.find('beds').text, > bathrooms = props.find('baths').text, > garage = props.find('garage').text, > area = props.find('surface_area'), > sqmt = area.find('built').text, > energy_rate = props.find('energy_rate').text, > realtor = self.request.user, > img = props.find('images'), > image = img.get('id=1'), > image_url = image.find('url').text, > photo_main_url = image_url, > video_url = props.find('video_url').text, > virtual_tour = props.find('virtual_tour').text, > ) > if created: > return listing.save() > > > > If you could help me and point me in the right direction it would be great. > > Thanks and sorry for the black background! > Thanks for the reply. XML Example: 3 2150 2018-06-27 11:18:37 B
parse xml from url to database
Hi there. I´m having some problems on parsing a real estate xml to database. The user profile have url field to import properties automatically from xml feed (url). The user can insert mannually or automatically, if the user insert the url feed then it should be able to get the objects and save them to mysql database automatically. The function is not working and i do not know why, gives no error but don´t populate the database. Models.py: class Listing(models.Model): '''Create table listing in db ''' realtor = models.ForeignKey(User, on_delete=models.CASCADE) title = models.CharField(max_length=200) address = models.CharField(max_length=200) city = models.CharField(max_length=100) state = models.CharField(max_length=100) zipcode = models.CharField(max_length=20) longitude = models.DecimalField(max_digits=9, decimal_places=6,default=0) latitude = models.DecimalField(max_digits=9, decimal_places=6, default=0) description = models.TextField(blank=True) price = models.IntegerField() bedrooms = models.IntegerField() bathrooms = models.DecimalField(max_digits=2, decimal_places=1) garage = models.IntegerField(default=0) sqmt = models.IntegerField(blank=True) lot_size = models.DecimalField(max_digits=5, decimal_places=1,blank=True) On_Processing = 'On Processing' AMais = 'A+' A = 'A' B = 'B' BMenos = 'B-' C = 'C' D = 'D' E = 'E' F = 'F' ENERGY_RATE_CHOICES = [ (On_Processing, 'On Processing'), (AMais, 'A+'), (A, 'A'), (B, 'B'), (BMenos, 'B-'), (C, 'C'), (D, 'D'), (E, 'E'), (F, 'F'), ] energy_rate = models.CharField( max_length=12, choices=ENERGY_RATE_CHOICES, default=On_Processing, ) photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/', default= 'default.jpg') photo_main_url = models.URLField(blank = True) is_published = models.BooleanField(default=True) list_date = models.DateTimeField(default=datetime.now, blank=True) video_url = models.CharField(max_length=100, blank=True) virtual_tour = models.URLField(blank=True) visit_num = models.PositiveIntegerField(default=0) def __str__(self): return self.title def get_absolute_url(self): return reverse('listing-detail', kwargs={'pk': self.pk}) Function on xml_parse.py: ... import xml.etree.ElementTree as ET import requests from users.models import Profile def xml_parse(self): prof_list = Profile.objects.all() for prof in prof_list: if prof.xml_ky is not None: url = xml_ky response = request.get(url) with open('xml/%s' % self.id + '/feed.xml', 'wb') as file: file.write(response.content) tree = ET.parse('xml/%s' % self.id + '/feed.xml') root = tree.getroot() for props in root.iter('property'): listing, created = Listing.object.create( title = props.find('type').text + ' ' + props.find('town').text, address = props.find('location_detail').text, city = props.find('town').text, state = props.find('province').text, zipcode = props.find('zipcode').text, loc = props.find('location'), longitude = loc.find('longitude').text, latitude = loc.find('latitude').text, desc = props.find('desc'), description = desc.find('en').text, price = props.find('price').text, bedrooms = props.find('beds').text, bathrooms = props.find('baths').text, garage = props.find('garage').text, area = props.find('surface_area'), sqmt = area.find('built').text, energy_rate = props.find('energy_rate').text, realtor = self.request.user, img = props.find('images'), image = img.get('id=1'), image_url = image.find('url').text, photo_main_url = image_url, video_url = props.find('video_url').text, virtual_tour = props.find('virtual_tour').text, ) if created: return listing.save() If you could help me and point me in the right direction it would be great. Thanks and sorry for the black background! -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f00d6ae6-eb29-462d-ab48-3384848d3514%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.