Skip to content

Instantly share code, notes, and snippets.

@dafma
Last active July 20, 2017 20:40
Show Gist options
  • Save dafma/5b4b734f39bbbbfc2a3f6a144e21b036 to your computer and use it in GitHub Desktop.
Save dafma/5b4b734f39bbbbfc2a3f6a144e21b036 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
{% regroup cities by pais as country_list %}
<ul>
{% for pais in country_list %}
<li>{{ pais.grouper }}</li>
<ul>
{% for city in pais.list %}
<li>{{ city.nombre }}: {{ city.population }}</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
</body>
</html>
------------------------------------------------------------
class cities(models.Model):
nombre = models.CharField(max_length=20)
poblacion = models.IntegerField()
pais = models.CharField(max_length=10)
def __str__(self):
return self.nombre
------------------------------------------------------------------
from django.conf.urls import url
from django.contrib import admin
from app import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index, name='index')
]
-----------------------------------------------------------------
def index(request):
ciudades = cities.objects.all()
context = {
"cities":ciudades
}
return render(request, 'index.html', context)
------------------------------------------------------------------- resultado
Mexico
Ciudad de México:
Toluca:
Chile
Santiago:
Murcia:
Argentina
La Plata:
Buenos Aires:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment