简体   繁体   中英

how to perform a query with filter in the django template

hello family by the way I am a beginner in web programming with the python language and its django framework

my concern is to be able to display the information coming from the join of several tables or models in my case I have the following tables:

Country table city table church table

but I used the mptt for country and city and everything is working fine but I want to display the list of all the countries with their respective cities as well as all the churches for each city. this is where the great difficulty lies for the month. If there is someone who can help me I will be very very happy

Have something like this in your view, where you pass in your tables:

def TestView(request):
    #...
    countries = CountryModel.objects.all()
    cities = CitiesModel.objects.all()
    churches = ChurchesModel.objects.all()
    #...

And then in your template, you can format these to seem like they are part of the same table. Use for loops to go through the models in your database. For example something like this should make a table with all of the items (assuming name is a parameter of the model:

<table>
    <tr>
        <td>Countries: </td>
        {% for country in countries %}
            <td>{{ country.name}}</td>
        {% endfor %}
    </tr>
    <tr>
        <td>Cities: </td>
        {% for city in cities %}
            <td>{{ city.name}}</td>
        {% endfor %}
    </tr>
    <tr>
        <td>Churches: </td>
        {% for church in churches %}
            <td>{{ church.name}}</td>
        {% endfor %}
    </tr>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM