mysite/urls
from django.conf.urls import url, include
from django.contrib import admin
from . import views
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^welcome/$', views.welcome),
url('', include('misterli.urls')),
mysite/views
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template import loader
def welcome(request):
if 'user_name' in request.GET and request.GET['user_name'] != '':
return HttpResponse('Welcome!~'+request.GET['user_name'])
else:
return render_to_response('welcome.html', locals())
mysite/misterli/urls
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^home/$', views.home, name='home'),
#url(r'^home/(\d{1,5})/$', views.home),
url(r'^restaurants_list/$', views.list_restaurants, name='list_restaurants'),
url(r'^comment/(\d{1,5})/$', views.comment),
]
mysite/misterli/views
from django.shortcuts import render_to_response
from misterli.models import Restaurant, Food, Comment
from django.http import HttpResponseRedirect
from django.utils import timezone
from django.template import RequestContext
import datetime
import time
import calendar
# Create your views here.
from typing import Any
def list_restaurants(request):
restaurants = Restaurant.objects.all()
return render_to_response('restaurants_list.html', locals())
'''def home(request, id):
if id:
restaurant = Restaurant.objects.get(id=id)
return render_to_response('menu.html', locals())
else:
return HttpResponseRedirect("/restaurants_list/")'''
def home(request):
if 'id' in request.GET and request.GET['id'] != '':
restaurant = Restaurant.objects.get(id=request.GET['id'])
return render_to_response('menu.html', locals())
else:
return HttpResponseRedirect("/restaurants_list/")
def comment(request, id):
if id:
r = Restaurant.objects.get(id=id)
else:
return HttpResponseRedirect("/restaurants_list/")
errors=[]
if request.POST:
visitor = request.POST['visitor']
content = request.POST['content']
email = request.POST['email']
date_time =datetime.datetime.now()
if any(not request.POST[k] for k in request.POST):
errors.append('* 有空白欄位,請不要留空')
if '@' not in email:
errors.append('* email格式不正確,請重新輸入')
if not errors:
Comment.objects.create(
visitor=visitor,
email=email,
content=content,
date_time=date_time,
restaurant=r
)
vistor, emali, content = ('', '', '')
return render_to_response('comments.html', locals())
mysite/misterli/models
from django.db import models
# Create your models here.
from typing import Any
class Restaurant(models.Model):
name = models.CharField(max_length=20)
phone_number = models.CharField(max_length=15, blank=True)
address = models.CharField(max_length=50, blank=True)
def __unicode__(self):
return self.name
class Food(models.Model):
name = models.CharField(max_length=20)
price = models.DecimalField(max_digits=3, decimal_places=0)
comment = models.CharField(max_length=50, blank=True)
is_spicy = models.BooleanField(default=False)
restaurant = models.ForeignKey(Restaurant)
def __unicode__(self):
return self.name
class Comment(models.Model):
content = models.CharField(max_length=245)
visitor = models.CharField(max_length=255)
email = models.EmailField(max_length=255)
date_time = models.DateTimeField()
restaurant = models.ForeignKey(Restaurant)
def __unicode__(self):
return self.name
mysite/misterli/admin
from django.contrib import admin
from misterli.models import Restaurant, Food, Comment
class RestaurantAdmin(admin.ModelAdmin):
list_display = ('name', 'phone_number', 'address') # type: Any
search_fields = ('name',)
class FoodAdmin(admin.ModelAdmin):
list_display = ('name', 'restaurant', 'address')
list_filter = ("is_spicy",)
ordering = ('-price',)
admin.site.register(Comment)
admin.site.register(Restaurant)
admin.site.register(Food)
# Register your models here.
mysite/templates/welcome.html
<html>
<head>
<title>Welcome</title>
</head>
<body>
<form action="" method="get">
<label for="user_name">您的名字</label>
<input id="user_name" type="text" name="user_name">
<input type="submit" value="進入網站">
</form>
</body>
</html>
mysite/misterli/templates/comments.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Comment</title>
</head>
<body>
<h2>{{r.name}}的評價</h2>
{% if r.comment_set.all %}
<p>目前共有{{r.comment_set.all|length}}個評價者</p>
<table>
<tr>
<tr>留言者</tr>
<tr>時間</tr>
<tr>評價</tr>
</tr>
{% for c in r.comment_set.all %}
<tr>
<td>{{c.visitor}}</td>
<td>{{c.date_time|date:"F j, Y"}}</td>
<td>{{c.content}}</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>無評價</p>
{% endif %}
<br /><br />
{% for e in errors %}
<p style="color:red;">{{e}}</p>
{% endfor %}
<form action="" method="post">
<table>
<tr>
<td><label for="visitor">留言者:</label></td>
<td><input id="visitor" type="text"name="visitor" value="{{visitor}}"></td>
</tr>
<tr>
<td><label for="email">電子郵箱:</label></td>
<td><input id="email" type="'text" name="email" value="{{email}}"></td>
</tr>
<tr>
<td><label for="content">評價:</label></td>
<td>
<textarea id="content" rows="10" cols="48" name="content"}>{{content}}</textarea>
</td>
</tr>
</table>
<input type="submit" value="給予評價">
{% csrf_token %}
</form>
</body>
</html>
mysite/misterli/templates/menu.html
<!doctype html>
<html>
<head>
<title>Menu</title>
<meta charset='utf-8'>
</head>
<body>
<h2>{{restaurant.name}} 的Menu</h2>
{% if restaurant.food_set.all %}
<p>本餐廳共有{{restaurant.food_set.all|length}}道菜</p>
<table>
<tr>
<th>菜名</th>
<th>價格</th>
<th>註解</th>
<th>辣不辣</th>
</tr>
{% for food in restaurant.food_set.all %}
<tr>
<td>{{food.name}}</td>
<td>{{food.price}}</td>
<td>{{food.comment}}</td>
<td>{% if food.is_spicy %}辣{% else %}不辣{% endif %}</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>本餐廳啥都不賣!</p>
{% endif %}
</body>
</html>
mysite/miserli/templates/restaurants_list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Restaurants</title>
</head>
<body>
<h2>餐廳列表</h2>
<form action="/home/" method="get">
<table>
<tr>
<th>選取</th>
<th>店名</th>
<th>電話</th>
<th>地址</th>
<tr>評價</tr>
</tr>
{% for r in restaurants %}
<tr>
<td><input type="radio" name="id" value="{{r.id}}"></td>
<td>{{r.name}}</td>
<td>{{r.phone_number}}</td>
<td>{{r.address}}</td>
<td><a href="/comment/{{r.id}}">評論</a></td>
</tr>
{% endfor %}
</table>
<input type="submit" value="觀看menu">
</form>
</body>
</html>