Zagorodniy Olexiy
Zagorodniy Olexiy

Reputation: 2212

Right way to create urls in Django

In Django I have my app where I place information about countries and cities of these countries. This is my model.py file:

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

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

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})

this is my urls.py:

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

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

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

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})

Here is my urls.py for city detail:

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

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

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

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

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.

Here is my url.py from my project

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

Please help me understand why this happens, thank you.

Upvotes: 4

Views: 1231

Answers (1)

Mathias Rav
Mathias Rav

Reputation: 2973

Since your project includes your app's URLs under the country/ prefix, the city page is available as country/spain/barcelona, or http://domain.com/country/spain/barcelona.

Upvotes: 3

Related Questions