I am building an django app that displays real estate data of different cities. The city info is saved in the django model. I have one view that displays all data of a particular city, I only need a way to get the user input of which city data he wants to see.
In the navbar are the names of the cities, how do I save the user choice in the navigation in the session info and use it as input for the view?
In a form of a POST?
base.html
<div >
<ul>
<li>
<a href="#">
<span >City 1</span>
</option>
</a>
</li>
<li>
<a href="#">
<span >City 2</span>
</a>
</li>
<li>
<a href="#">
<span >City 3</span>
</a>
</li>
<li>
<a href="#">
<span >City 4</span>
</a>
</li>
</ul>
</div>
CodePudding user response:
You can create a new View like /city/str:cityname, get the info about city and use your existing template that render details about city.
in your base.html, you can put the href like this city/City1. It should make the get request to this view /city/str:cityname.
CodePudding user response:
You can use get request to the views when one of the option is clicked. For example:
In the base.html navigtion section, your list item will be like
<li>
<a href="{% url 'get-city-data' city=city2 %}">
<span >City 2</span>
</a>
</li>
so your urls.py be something like
urlpatterns = [
# your existing urls
path('get-city-data/<str:city>/', get_city_data, name='get-city-data'),
]
And views be like
def get_city_data(request, city=None):
# your logic here to return data filter by city
context = {} # populate with required context
return render(request, 'city.html', context)