python - Django 表单、 View 和 forms.py 不能一起工作

标签 python django django-forms

我第一次涉足 Django,允许用户输入星期几,然后在我的数据库中搜索在给定日期营业的餐馆。现在,餐厅对象 (Resto) 有一个 days_open 属性,每一天都用逗号分隔(星期一、星期二等...)。

当我输入一天时,生成的页面只显示标题和“[]”,我似乎无法让它返回 Resto 对象列表。显示方括号的原因是什么?如何修复它以显示搜索结果?

代码如下 - 如果我忘记包含任何相关位,我深表歉意。

我的表格.py:

from django import forms
from .models import Resto

class RestoSearch(forms.ModelForm):
    class Meta: 
        model = Resto
        fields = ('title', 'description', 'opening_hour', 'closing_hour')

模型.py:

from django.db import models

class Resto(models.Model):
    title = models.CharField(max_length=300)
    description = models.TextField()
    opening_hour = models.TimeField(auto_now=False, auto_now_add=False, null=True)
    closing_hour = models.TimeField(auto_now=False, auto_now_add=False, null=True)
    days_open = models.TextField(blank=True)

views.py:

from django.shortcuts import render
from django.http import Http404
from django.shortcuts import HttpResponse
from belize.models import Resto
from django.core.exceptions import *
from .forms import RestoSearch

def index(request):
    return render(request, 'form.html')

def search(request):

    form = RestoSearch()

    if request.method == 'POST':
        search_id=request.POST.get('textfield', None)
        try:
            #I think this is where my problem is

            available = Resto.objects.filter(days_open = search_id)

            html = ("<H1>Hello</H1>", available)
            return HttpResponse(html)
        except Resto.DoesNotExist:
            return HttpResponse("Please try another day")  
        else:
            return render(request, 'belize/form.html')


def restaurant_detail(request, id):
    try:
        restaurant = Resto.objects.get(id=id)
    except Resto.DoesNotExist:
        raise Http404('This restaurant does not exist')
    return render(request, 'belize/restaurant_detail.html', {
        'restaurant': restaurant,
        })

模板 form.html:

<form method="POST" action="/search/"> 
{% csrf_token %}
<input type="text" name="textfield">

<button type="submit">Enter a day of the week</button>
</form>

最佳答案

我假设您要显示的是 RestoForm,在这种情况下索引方法不正确。应该是

def index(request):
    form = RestoForm()
    return render(request, 'form.html', {'form': form })

然后你的模板应该改变为

<form method="POST" action="/search/"> 
{% csrf_token %}
{{ form }}

<button type="submit">Enter a day of the week</button>
</form>

有关更多详细信息,请参阅 https://docs.djangoproject.com/en/1.9/topics/forms/#the-template 中的示例

关于python - Django 表单、 View 和 forms.py 不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37847799/

相关文章:

python - 如果图像具有 (28,28,3) 形状,我如何将其转换为 (28.28,1)?

python - 在 Python 中产生和等待子进程

python - 如何在列表上触发 Traits 静态事件通知?

python - Django:作为值列表查询一部分的相关模型值列表

django - 在 django 项目中配置谷歌云数据存储

python - ModelForm 的 save 方法中的 Django admin.py : save_model() is not called by model. save()

django:如何仅访问 form.errors 中的第一条错误消息?

python - 防止 scipy 特征向量因计算机而异

python - 在Python中扩展内部类属性

python - 在Django模板中以另一条记录的字段内容为标签显示未知数量的字段