0

I want to create a generic country view that populates data based on the what country i clicked on a map. The current URL looks like:

# Country View URL 
   url(r'^country/' , 'wiki.views.country',
        name = 'wiki_country'),

And the view is:

def country(request):
    return render_to_response("wiki/country.html")

This is fine if I want a separate page for each country. I was reading elsewhere that there is no simple way to get a variable from a template in Django. What I would want is for the link in the template not only have the URL, but also a value ("Country Name") that then allows me to dynamically populate a single countryView template.

1 Answer 1

1

For specific country which will take id and name of country you can define the URL pattern as:

url(r'^country/(?P<id>\d+)/(?P<name>\w+)' , 'wiki.views.country', name='wiki_country'),

The view will be:

def country(request, id, name):
    country = Country.objects.get(id=id, name=name)
    return render_to_response("wiki/country.html", {'country': country})

In listing template you can have a link as:

{% for country in countries %}
    <a href="{% url 'wiki_country' country.id country.name %}">{{ country.name }}</a>
{% endfor %}

Hope this will lead you somewhere.

Sign up to request clarification or add additional context in comments.

2 Comments

BTW, do I need {{country.name}} in the listing template? Since this is the place the user will select the country, ought I not to just hard code it?
Ok, I think this will work. Still messing about with the model, but my question is where does the name and id of the country go so that the instance can be made based on those values? In the block on the template surely, correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.