Right way to create urls in Django
<p>In Django I have my app where I place information about countries and cities of these countries. This is my model.py file:</p>

<pre><code>class Country(models.Model):
        class Meta:
                verbose_name_plural = u'Countries'

        name = models.CharField(max_length=50)
        slug = models.CharField(max_length=255)
        description = models.TextField(max_length=10000, blank=True)

        def __unicode__(self):
                return self.name

class City(models.Model):
        class Meta:
                verbose_name_plural = u'Cities'

        name = models.CharField(u'city', max_length=200)
        slug = models.CharField(max_length=255, blank=True)
        description = models.TextField(max_length=10000, blank=True)
        country = models.ForeignKey('Country', blank=True, null=True)

        def __unicode__(self):
                return self.name
</code></pre>

<p>I have the detail view of my country, in this view there is a list of cities of this country(views.py):</p>

<pre><code>def CountryDetail(request, slug):
        country = get_object_or_404(Country, slug=slug)
        list_cities = City.objects.filter(country=country)
        return render(request, 'country/country.html', {'country':country, 'list_cities':list_cities})
</code></pre>

<p>this is my urls.py:</p>

<pre><code>url(r'^(?P&lt;slug&gt;[-_\w]+)/$', views.CountryDetail, name='country'),
</code></pre>

<p>I want to create a url of cities which contain a slug of the country and a slug of the city, for example <code>domain.com/spain/barcelona/</code>.</p>

<p>So I created the detail view of the city, and it's looks like this:</p>

<pre><code>def CityDetail(request, resortslug):
        country = Country.objects.get(slug=countryslug)
        city = get_object_or_404(City, country=country, slug=cityslug)
        return render(request, 'country/city.html', {'country':country, 'city':city})
</code></pre>

<p>Here is my urls.py for city detail:</p>

<pre><code>url(r'^(?P&lt;countryslug&gt;[-_\w]+)/(?P&lt;cityslug&gt;[-_\w]+)$', views.CityDetail, name='resort'),
</code></pre>

<p>And this is how it looks like in my html file detail of the country that links to the cities:</p>

<pre><code>&lt;h1&gt;{{country.name}}&lt;/h1&gt;
&lt;p&gt;{{country.description}}&lt;/p&gt;
&lt;h2&gt;Cities&lt;/h2&gt;
{% for city in list_cities %}
   &lt;a href="/{{country.slug}}/{{city.slug}}"&gt;
      &lt;p&gt;{{city.name}}&lt;/p&gt;
   &lt;/a&gt;
{% endfor %}
</code></pre>

<p>But when I click on the link of the url of the city, I get a 404 error.</p>

<pre><code>Page not found (404)
Request Method: GET
Request URL:    http://domain.com/spain/barcelona
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
The current URL, spain/barcelona, didn't match any of these.
</code></pre>

<p>Here is my url.py from my project</p>

<pre><code>urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^country/', include('country.urls')),
</code></pre>

<p>Please help me understand why this happens, thank you.</p>