使用地理定位的Django标记

时间:2017-05-25 11:35:42

标签: python django

我使用django-geoposition来管理我的地理数据。 在我的home.html:

<!-- Add Google Maps -->
<script>
function myMap() {
  {% for posts in post_list %}
  myCenter=new google.maps.LatLng(34.8402781,135.592376);
  var myLocation=new google.maps.LatLng({{posts.position.latitude}},{{posts.position.longitude}});
  var mapOptions= {
    center:myCenter,
    zoom:7, scrollwheel: true, draggable: true,
    mapTypeId:google.maps.MapTypeId.HYBRID
  };
  var map=new google.maps.Map(document.getElementById("googleMap"),mapOptions);
  var marker = new google.maps.Marker({
    position: myLocation,
    title:'{{ posts.title }}',
    draggable: false,

  });
  marker.setMap(map);

  {%endfor%}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCB1CA2DoITfJoFY4HWTLTxH4Avx7QWWqA&callback=myMap"></script>

但它只是在地图上显示了第一个帖子的制作者。我猜for循环不起作用。我不知道该怎么办。我将不胜感激任何帮助!

models.py

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

class Post(models.Model):
    title = models.CharField(max_length=100)
    introduction = models.TextField(blank=True)
    reason = models.TextField(blank=True)
    transportation = models.TextField(blank=True)
    basic_info = models.TextField(blank=True)
    photo = models.URLField(blank=True)
    location = models.CharField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True)
    position = GeopositionField(blank=True)
    website = models.URLField(blank=True)
    useful_link = models.URLField(blank=True)

def __str__(self):
 return self.title

views.py:

from django.shortcuts import render
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .models import Post

def home(request):
    posts = Post.objects.all()
    paginator = Paginator(posts, 6) #每页显示十个
    page = request.GET.get('page')
    try :
        post_list = paginator.page(page)
    except PageNotAnInteger :
        post_list = paginator.page(1)
    except EmptyPage :
        post_list = paginator.paginator(paginator.num_pages)
    return render(request, 'attractions/home.html', {'post_list': post_list,})

def post_detail(request, pk):
    post = Post.objects.get(pk=pk)
    return render(request, 'attractions/post.html', {
    'post': post,
})

景点\ urls.py

from django.conf.urls import url
from . import views
app_name = 'attractions'
urlpatterns = [
    url(r'^$',views.home, name = 'home'),
    url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail')
]

urls.py:

from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^',include('attractions.urls')),
    url(r'^markdown/', include( 'django_markdown.urls')),
]

admin.py:

from django.contrib import admin
from .models import Post
from django_markdown.admin import MarkdownModelAdmin
from django_markdown.widgets import AdminMarkdownWidget
from django.db.models import TextField

class PostAdmin(MarkdownModelAdmin):
    list_display = ('title','position',) #顯示欄
    search_fields = ('title',) #搜索欄
    ordering = ('created_at',) #減號表示降序
    formfield_overrides = {TextField: {'widget': AdminMarkdownWidget}}

admin.site.register(Post,PostAdmin)

1 个答案:

答案 0 :(得分:0)

我看到以下错误:
  - Javascript:你必须为标记做循环而不是整个地图渲染javascript代码 我建议你使用基于类的视图(在本例中为ListView)。