1

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',)
4

1 回答 1

1

Django 通常指向RESTful应用程序,因此每个现有对象都应该有自己的 URL 来编辑(即使它是 AJAX)。如此好的 URL 看起来像是/obj/123/edit/用于编辑现有对象和/obj/create/ 创建新对象。实际上,在完美的 REST 中,您可以对所有 CRUD 活动使用很少的 URL,但这也足够了。因此,您的 URL 中有对象 ID,无需在表单中复制它。

或者,您始终可以在表单中显示隐藏的输入,value="{{ form.instance.pk }}"并在视图中手动处理它。

于 2012-05-23T03:01:51.533 回答