Reputation: 611
I'm wading – carefully – into some basic geolocation using HTML5. I currently have a meta form for my first Django application that has space for lat and long coordinates and have found the proper code to obtain those coordinates using the Google Maps API (pretty simple stuff).
Next step: inserting those coordinates automatically into the meta form when a user access the application. The application would ideally allow users to make posts and store their coordinates for future reference and filtering (that part is the big endeavor; one step at a time).
Using JavaScript (which I know very little) with Django in this manner seems to be the most efficient manner to accomplish this and was looking to see if there's a straightforward method for doing this. I found some ways to accomplish this that may work using jQuery, but with the meta form automatically setting up the form's structure it doesn't seem as if there's a simple way to add an "id" to the form (researched but can't seem to find a means).
Any insight or experience that can shared would be greatly appreciated.
Model:
class Story(models.Model):
objects = StoryManager()
title = models.CharField(max_length=100)
topic = models.CharField(max_length=50)
copy = models.TextField()
author = models.ForeignKey(User, related_name="stories")
zip_code = models.CharField(max_length=10)
latitude = models.FloatField(blank=False, null=False)
longitude = models.FloatField(blank=False, null=False)
date = models.DateTimeField(auto_now=True, auto_now_add=True)
pic = models.ImageField(upload_to='pictures', blank=True)
caption = models.CharField(max_length=100, blank=True)
def __unicode__(self):
return " %s" % (self.title)
Form:
class StoryForm(forms.ModelForm):
class Meta:
model = Story
exclude = ('author',)
Upvotes: 1
Views: 1252
Reputation: 5841
Django is usually pointed towards RESTful apps so every existing object should have its own URL to edit (even if it's AJAX). So good URL will look something like /obj/123/edit/
for editing existing object and /obj/create/
for creating a new one. Actually, in perfect REST you can use very few URLS for all CRUD activity, but this is good enough too. So you have object ID in your URL and don't need to duplicate it in your form.
Or you can always display a hidden input in your form with value="{{ form.instance.pk }}"
and manually process it in the view.
Upvotes: 1