2

Django で GeopositionField の緯度と経度を初期化するにはどうすればよいのでしょうか。

これは私のクラスです:

class Business(models.Model):
    name = models.CharField(max_length=64)
    address = models.CharField(max_length=64)
    district = models.CharField(max_length=32)
    city = models.CharField(max_length=32)
    phone = models.CharField(max_length=32)
    category = models.ManyToManyField(Category)
    picture = models.ImageField(upload_to='business_image', blank=True, null=True)
    slug = models.SlugField(unique=True)
    pub_date = models.DateField(blank=True, auto_now_add=True)
    position = GeopositionField()

    MY_CHOICES = (('$', '$'),
                  ('$$', '$$'),
                  ('$$$', '$$$'),
                  ('$$$$', '$$$$'),)
    price = models.CharField(max_length=4, choices=MY_CHOICES)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Business, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.name

これは私のビューファイルです:

def new_biz(request):
    args = {'user': request.user}
    if request.method == 'POST':
        form = BusinessForm(request.POST, request.FILES)
        if form.is_valid():
            the_business = form.save(commit=False)
            the_business.picture = form.cleaned_data['picture']
            the_business.save()
            # todo Change the return address
            return HttpResponseRedirect('/restaurants')
        else:
            # print form.is_valid()
            # print form.errors
            args['errors'] = form.errors
            args.update(csrf(request))
            args['form'] = BusinessForm(request.POST)
    else:
        # user = request.user
        # profile = user.profile
        # form = UserProfileForm(instance=profile)
        args.update(csrf(request))
        ff = BusinessForm(initial={'position.Latitude': 5})
        args['form'] = ff
        # print (ff.Position)
        for i in ff:
            print i
            # print i.type

    return render_to_response('/new_biz.html', args)

そして、これは私のテンプレートです:

<div class="clearfix layout-block layout-right-rail">
                        <div class="column column-alpha ">
                            <div class="duplicate-business-suggestions hidden" data-component-bound="true">
                                <p><strong>Are you thinking of one of these businesses?</strong></p>

                                <div class="biz_matches"></div>
                            </div>
                            <ul>
                                <link href="/static/geoposition/geoposition.css" type="text/css" media="all"
                                      rel="stylesheet"/>
                                <script type="text/javascript" src="//maps.google.com/maps/api/js?sensor=false"></script>
                                <script type="text/javascript" src="/static/geoposition/geoposition.js"></script>

                                {% for field in form %}
                                    {% csrf_token %}
                                        <div  style="display: inline-block;font-weight: bold;
                                                margin: 0 0 6px; {% if field.errors %}color: #c41200{% endif %};
                                                font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px;">
                                            {{ field.name }} <br>
                                        </div>

                                    {{ field.help_text }}
                                    {{ field }}
                                {% endfor %}



                                <li class="submit-review">
                                    <button id="review-submit-button" type="submit" value="submit"
                                            class="ybtn ybtn-primary ytype"><span>Add Business</span></button>


                                    <span class="submit-in-progress hidden">Adding business…&lt;/span>
                                    <input type="hidden" name="default_action" value="select">
                                    <input type="hidden" name="action_select" value="select" id="rating-form-action">
                                </li>
                            </ul>


                        </div>

マップのデフォルトの場所 (ニューヨークなど) にマーカーを配置し、ズーム レベルを定義したいと考えています。

4

1 に答える 1

2

設定ファイル内でGEOPOSITION_MAP_OPTIONSGEOPOSITION_MARKER_OPTIONSを指定する必要があります。

また、モデル定義内で GeopositionField のデフォルト値を指定することもできます:

from django.db import models
from geoposition import Geoposition
from geoposition.fields import GeopositionField

class Business(models.Model):
        name = models.CharField(max_length=64)
        position = GeopositionField(default=Geoposition(40.77, 73.98))
于 2015-06-11T09:40:47.430 に答える