i have 2 models:
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, when create world few countries inside have view:
def my_world(request, pk): world = get_object_or_404(world, pk=pk) return render(request, 'game/my_world.html', {'world': world})
and html file:
{% country in world.country.all %} <a href="{% url 'game.views.delete_country' pk=country.pk %}">{{ country.name }}</a> {% endfor %}
delete view:
def delete_country(request, pk): country = get_object_or_404(country, pk=pk) country.delete() return render(request, 'game/my_world.html', {'world': world})
url:
url(r'^world/(?p<pk>[%&+ \w]+)/$', views.my_world), url(r'^world/(?p<pk>[%&+ \w]+)/$', views.delete_country, name='delete_country'),
that's invented
now have in my_world list of countries , when click 1 of deleted. want change it:
- how change delete country choosen world within world?
- how after automatically return my_world?(probably primary key current option not work)
if don't want delete country altogether, remove specific world, need way of identifying world mean. you'll need in url:
url(r'^world/(?p<world_pk>\d+)/(?p<country_pk>\d+)/$', views.delete_country, name='delete_country'),
and can link in template with:
{% url 'game.views.delete_country' world_pk=world.pk, country_pk=country.pk %}
now view can world , remove specific country. note answer second question use redirect
shortcut:
def delete_country(request, world_pk, country_pk): world = get_object_or_404(world, pk=pk) country = get_object_or_404(country, pk=pk) world.country.remove(country) return redirect("my_world", pk=world.pk)
also note it's pretty bad idea have delete action triggered simple link. action modifies database - 1 deletes - should done via form post.
Comments
Post a Comment