我希望创建一个非常简单的表单,其中有几个下拉字段,允许用户查询我创建的餐馆数据库(我想使用字段'food','city'和'average rating')。虽然我找到了使用单独的html页面(Django form to query database (models))来实现这一点的方法,但我想在一个页面(restaurants.html)上实现这一点。
我正在使用Python 3.1.0,Django 2.0.1和Bootstrap 4.0。
感谢您的所有帮助。
我的models.py
如下:
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 %}