python - Django view - exclude in queryset? -
i have problem: after creating new world, (all countries automatically assigned every new world) user choose 1 country showed on top of site , can't delete country. how this?
class country(models.model): name = models.charfield(verbose_name="name of country", max_length=100, default="australia") number = models.integerfield(verbose_name="number of country", default="1") def __unicode__(self): return self.name class world(models.model): country = models.manytomanyfield(country) name = models.charfield(verbose_name="new map", max_length=100) def __unicode__(self): return self.name
now have this: after creating world user redirecting new_my_world, there should choose country , redirected site my_world view can delete countires expect one. how this? views.py
def new_my_world(request, pk): world = get_object_or_404(world, pk=pk) return render(request, 'game/new_my_world.html', {'world': world}) def my_world(request, pk): world = get_object_or_404(world.objects.exclude(??, pk=pk) return render(request, 'game/my_world.html', {'world': world}) def choose_country(request, world_pk, country_pk): world = get_object_or_404(world, pk=pk) country = get_object_or_404(country, pk=pk) return redirect('my_world', pk=world.pk)
i know in my_world view there's should exclude (excluding coosen country) don't know how connect it.
edit
url:
url(r'^world/(?p<pk>[%&+ \w]+)/(?p<country_pk>[%&+ \w]+)/$', views.my_world, name='my_world'),
edit2:
how create url my_world? after creating new world,when click on first country choose error
with url:
url(r'^world/(?p<pk>[%&+ \w]+)/(?p<country_pk>[%&+ \w]+)/$', views.my_world, name='my_world'), page not found (404) request method: request url: http://127.0.0.1:8000/world/22/9/ raised by: game.views.my_world no world matches given query.
i fix choose_country following:
def choose_country(request, world_pk, country_pk): world = get_object_or_404(world, pk=world_pk) country = get_object_or_404(country, pk=country_pk) return redirect('my_world', pk=world.pk, country_pk = country.id)
then in my_world function should changed this:
def my_world(request, pk, country_pk): world = get_object_or_404(world.objects.exclude(country__id = country_pk), pk=pk) return render(request, 'game/my_world.html', {'world': world})
edit 1 if country same of country of world, above get_object_or_404(world.objects.exclude(country__id = country_pk), pk=pk)
sure return not found. suggest my_world follows:
def my_world(request, pk, country_pk): world = get_object_or_404(world, pk=pk) countries = get_object_or_404(country.object.exclude(pk=country_pk)) return render(request, 'game/my_world.html', {'world': world, 'countries': countries})
Comments
Post a Comment