我想知道如何在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…</span>
<input type="hidden" name="default_action" value="select">
<input type="hidden" name="action_select" value="select" id="rating-form-action">
</li>
</ul>
</div>
我想将标记放置在地图上的默认位置(例如纽约),并具有定义的缩放级别。
最佳答案
您应该在设置文件中指定GEOPOSITION_MAP_OPTIONS和GEOPOSITION_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))