0

I am looking to create a very simple form with several drop down fields allowing users to query a database of restaurants I have created (I would like to use the fields 'food', 'city' and 'average rating'). Whilst I have found an approach to do this using separate html pages (Django form to query database (models)) I would like to implement this on one page (restaurants.html).

I am using Python 3.1.0, Django 2.0.1 and Bootstrap 4.0.

Appreciate all your help.

My models.pyis as follows:

from django.db import models
import numpy as np

# Create your models here.
class Restaurant(models.Model):
    name = models.CharField(max_length=100, null=False)
    desc = models.CharField(max_length=100)
    web = models.CharField(max_length=100)
    phone = models.CharField(max_length=40)
    address = models.CharField(max_length=100)
    post_code = models.CharField(max_length=20)
    picture = models.ImageField(upload_to='images/restaurants', null=True)
    map = models.ImageField(upload_to='images/restaurants', null=True)

FOOD = ((1,'English'),
        (2,'French'),
        (3,'American'),
        (4,'Indian'),
        (5, 'Undefined'))
food = models.IntegerField(choices=FOOD, default=5)

CITY = ((1,'London'),
        (2,'Paris'))

city = models.IntegerField(choices=CITY, default=1)

STARS = ((1,'One'),
        (2,'Two'),
        (3,'Three'),
        (4,'Four'),)
CB_rating = models.IntegerField(choices=STARS, default=4)

def average_rating(self):
    all_ratings = map(lambda x: x.rating, self.review_set.all())
    return np.mean(all_ratings)

def __str__(self):
    return self.name

views.py:

from django.shortcuts import get_object_or_404, render
from .models import Review, Restaurant
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .forms import ReviewForm
import datetime

def review_list(request):
    latest_review_list = Review.objects.order_by('-pub_date')[:9]
    context = {'latest_review_list':latest_review_list}
    return render(request, 'review_list.html', context)


def review_detail(request, review_id):
    review = get_object_or_404(Review, pk=review_id)
    return render(request, 'review_detail.html', {'review': review})

def restaurant_list(request):
    restaurant_list = Restaurant.objects.order_by('-name')
    context = {'restaurant_list':restaurant_list}
    return render(request, 'restaurants.html', context)

def restaurant_detail(request, restaurant_id):
    restaurant = get_object_or_404(Restaurant, pk=restaurant_id)
    return render(request, 'restaurant_detail.html', 'restaurant':restaurant})

def user_review_list(request, username=None):
    if not username:
        username = request.user.username
    latest_review_list =     Review.objects.filter(user_name=username).order_by('-pub_date')
    context = {'latest_review_list':latest_review_list, 'username':username}
    return render(request, 'user_review_list.html', context)

restaurants.html:

{% extends 'base.html' %}
{% load staticfiles %}

{% block content %}
<div class="container">
    <div class="row">
        <div class="col-md-4 col-sm-offset-4" style="text-align: center">
          <h3>Search Criteria</h3>
          <hr>
        </div>
    </div>
</div>

<div class="container">
    <div class="row">
        <div class="col-md-4 col-sm-offset-4" style="text-align: center">
            <form method="get" action="/search/">
                Search Restaurants:<input type="text" name="q" id="id_q" value="{{ query }}"/>
                <input type="submit" value="Search" />
            </form>
        </div>
    </div>
</div>

<div class="container">
    <div class="row">
        <div class="col-md-4 col-sm-offset-4" style="text-align: center">
            <h3>Search Results</h3>
            <hr>
        </div>
    </div>
</div>

<div class="container">
  <div class="row">
    <div>
      {% if restaurant_list %}
      <div class="container">
      <div class="table-responsive">
        <table class="table table-bordered table-hover" style="text-align: center;">
          <caption class="text-center">A list of restaurants based on your search criteria above.</caption>
          <thead>
            <th class="col-xs-2">Name</th>
            <th class="col-xs-6">Description</th>
            <th class="col-xs-1" style="text-align: center">Food Type</th>
          </thead>
          <tbody>
            {% for restaurant in restaurant_list %}
            <tr>
              <td style="text-align: left;"><a href="{% url 'restaurants:restaurant_detail' restaurant.id %}"><strong>{{ restaurant.name }}</strong></a></td>
              <td style="text-align: left;">{{ restaurant.desc }}</td>
              <td style="text-align: center;">{{ restaurant.food }}</td>
            {% endfor %}
            </tr>
              </tbody>

        </table>
      </div>
    </div>
      {% else %}
      <h3>No restaurants match your search criteria.</h3>
      {% endif %}
    </div>
  </div>
</div>

{% endblock %}
dferenc
  • 7,918
  • 12
  • 41
  • 49
MSM
  • 1
  • Can you describe more? What exactly do you want to do? – seuling Jan 18 '18 at 10:05
  • I'm not sure what your problem is here, or why you think it requires separate pages. – Daniel Roseman Jan 18 '18 at 10:07
  • You would probably benefit a lot by leveraging Django's generic views. There are also many popular extensions that would probably fit your use case very nicely. Be sure to explore those options. It also doesn't look like you've using your form class in your views or templates. Most of this material should be covered in the django official tutorial. – sytech Jan 18 '18 at 10:09
  • in 'def restaurant_list' check for if request.method == "POST" : do search specific restaurant else if request.method == "GET": send all restaurant – Vaibhav Jan 18 '18 at 10:19
  • Thanks for the responses. Sorry if it was not clear - I am looking to replicate functionality similar to the following: https://www.timeout.com/london/restaurants/search-restaurants?category=Restaurants. I.e. having a series of fields that the user can select to narrow down the list of restaurants they see. – MSM Jan 18 '18 at 17:57

0 Answers0