Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

280
Views
Display time stored in database in HTML (django)

I'm building a bus booking website using Django. Users fill From, To, Bus type(ordinary or Volvo) and date. Results display the buses available on that route on that particular date. I made three tables - Bus, Route, and Frequency. You can check my models.py and views.py here - https://dpaste.de/Oi6a I convert the date to the corresponding day of the week. I have filtered out the buses, However, I want to display time on the template. This is part of my views.py code:

def select(request):

    bus_type = request.GET.get('bus_type')
    bus_from = request.GET.get('bus_from')
    bus_to = request.GET.get('bus_to')

    date_string = request.GET.get('date')
    date = datetime.strptime(date_string, '%Y-%m-%d')

    day = calendar.day_name[date.weekday()]

    kwargs = { '{0}__range'.format(day): ["00:00:00", "23:59:59"],}


    qs = Frequency.objects.filter(bus__type_of_bus=bus_type, bus__route__location_from=bus_from, bus__route__location_to=bus_to, **kwargs)

    context = {'qs': qs, 'date':date_string,}
    template = 'select.html'
    return render(request, template, context)

As you can see qs filters the buses available and is then passed to the template. This is a part of the template:

<div id="pricing-table" class="clear">
    {% for info in qs %}
    <div class="price_block">
        <h3>{{ info.bus.type_of_bus }}<span>{% if info.bus.type_of_bus == 'Volvo' %} Rs 550 {% else %}Rs 330 {% endif %}</span></h3>
        <a href="{% url 'book:seats' %}"rel="follow" rel="noreferrer" class="signup" type="submit">Book</a>
        <ul>
            <li><b>Bus Number -</b> {{ info.bus.bus_number }}</li>
            <li><b>Route -</b> {{ info.bus.route }}</li>
            <li><b>Date -</b> {{ date }}</li>
            <li><b>Time -</b> {{ info.day }}</li>
        </ul>
    </div>

In the last line of this HTML file, I have added info.day. What I want is that this displays the time that bus operates on the given day. For example, If a person searches a bus on 29th April. 29th April will be converted to corresponding day i.e. Friday. The frequency table has 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' and 'Sunday' as attributes and these all are TimeField(). qs filters the buses and passes to the template. I want {{ info.day }} to show the time when the bus operates. Instead, it shows nothing. But when I change {{ info.day }} to {{ info.Friday}} it start showing the time. How can I display the time without having to manually go and change the day in {{ info.(here) }} every time I search for a bus. I didn't make it very clear, but I hope you understand. Let me know if you don't.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can create a custom template tag do this for you and a method on the model.

I haven't tested this code, but this should work with or without small adjustments

Add this method to the Frequency model:

def get_day_info(self, date):
        # Where date_string would be a datestring
        date = datetime.strptime(date_string, '%Y-%m-%d')
        day = calendar.day_name[date.weekday()]
        info_time = getattr(self, '{}'.format(day))
        return info_time

And register this as a template tag.

@register.simple_tag
def get_day_info(info, date):
    return info.get_day_info(date)

You would call it in the template like this:

<li><b>Time -</b> {% get_day_info info {{ date }} %}</li>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!