0

I started working with Django earlier this week and watched a few tutorials and thought I would try out switching over a site I made in PHP to something a little more current. I ran into some issues over the weekend and managed to get help with some of them, but one issue remains.

I already have an existing database with some information it that I want to retrieve to display on a website. The information is modified through the database so Django doesn't have to deal with any of that - just displaying results to a bootstrapped template file.

So, in essence Django would have 2 models - one for each relevant database table.

The first model relates to products

class Bikes(models.Model):
    bikempn = models.CharField(primary_key=True, max_length=50)
    bikecategory = models.CharField(max_length=50)
    bikeyear = models.CharField(max_length=4)
    bikebrand = models.CharField(max_length=50)
    bikedesc = models.CharField(max_length=255)
    bikesize = models.CharField(max_length=50)
    bikecolour = models.CharField(max_length=255)
    bikeurl = models.CharField(max_length=255)

    class Meta:
        managed = False
        db_table = 'bikes'

The second model relates to their expected arrival dates. This model would represent a database view which performs some logic comparing product that is on its way to the number of matching items that are on reserve.

The model is:

class Eta(models.Model):
    bikempn = models.ForeignKey(Bikes, on_delete=models.CASCADE, primary_key = True, db_column='bikempn', related_name='etas')
    eta = models.DateField()

    class Meta:
        managed = False
        db_table = 'bike_eta'

The idea of this website is essentially to let people know if a product they are interested in will be arriving any time soon.

I have been trying to come up with a query that will display all of the linked information, but everything I find online hasn't quite worked. I have received some help along the way but have hit the wall again and frankly feel foolish having not figured this out yet.

So, currently I have a query that is:

kidsquery = Bikes.objects.filter(bikecategory='kids').select_related('etas')

This filters out only bikes categorized as kids bikes and should join the two models together.

My relevant template to display this information is:

{% extends 'base.html' %}

{% block content %}
    {% if kidsquery %}
    <div class="table-responsive">
        <table class="table table-striped table-hover table-bordered table-sm">
            <thead class="table-dark">
                <tr>
                    <th scope="col">Year</th>
                    <th scope="col">Brand</th>
                    <th scope="col">Model</th>
                    <th scope="col">Colour</th>
                    <th scope="col">Size</th>
                    <th scope="col">Part#</th>
                    <th scope="col">ETA</th>
                </tr>
            </thead>
         
            {% for info in kidsquery %}
            <tr>
                <td>{{ info.bikeyear }}</td>
                <td>{{ info.bikebrand }}</td>
                {% if info.bikeurl %}
                <td><a href="{{ info.bikeurl }}" target="_blank">{{ info.bikedesc }}</a></td>
                {% else %}
                <td>{{ info.bikedesc }}</td>
                {% endif %}
                <td>{{ info.bikecolour }}</td>
                <td>{{ info.bikesize }}</td>
                <td>{{ info.bikempn }}</td>
                {% for arrival in info.etas.all %}
                {% if arrival is null %}
                <td>Contact Us</td>
                {% else %}
                <td>{{ arrival|date:"F Y" }}</td>
                {% endif %}
                {% endfor %}

            </tr>
            {% endfor %}
            
        </table>
    </div>
    {% endif %}
    
{% endblock %}

Does anyone know why I cannot get anything to display in the final column (where it should display the eta date values (or failing that, the "Contact Us" result)?

1 Answer 1

0

The proper solution to pulling the information out in the template:

{% extends 'base.html' %}

{% block content %}
    {% if kidsquery %}
    <div class="table-responsive">
        <table class="table table-striped table-hover table-bordered table-sm">
            <thead class="table-dark">
                <tr>
                    <th scope="col">Year</th>
                    <th scope="col">Brand</th>
                    <th scope="col">Model</th>
                    <th scope="col">Colour</th>
                    <th scope="col">Size</th>
                    <th scope="col">Part#</th>
                    <th scope="col">ETA</th>
                </tr>
            </thead>
         
            {% for info in kidsquery %}
            <tr>
                <td>{{ info.bikeyear }}</td>
                <td>{{ info.bikebrand }}</td>
                {% if info.bikeurl %}
                <td><a href="{{ info.bikeurl }}" target="_blank">{{ info.bikedesc }}</a></td>
                {% else %}
                <td>{{ info.bikedesc }}</td>
                {% endif %}
                <td>{{ info.bikecolour }}</td>
                <td>{{ info.bikesize }}</td>
                <td>{{ info.bikempn }}</td>
                {% for arrival in info.etas.all %}
                {% if arrival.eta %}
                <td>{{ arrival.eta|date:"F Y" }} </td>
                {% else %}
                <td>Contact Us</td>
                {% endif %}
                {% endfor %}

            </tr>
            {% endfor %}
            
        </table>
    </div>
    {% endif %}
    
{% endblock %}

Swapped around the checks, and compensated for the lack of a date.

Not the answer you're looking for? Browse other questions tagged or ask your own question.