Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 为什么我不能在我的html模板上获得下拉列表?_Django_Django Models_Django Forms_Django Templates_Django Views - Fatal编程技术网

Django 为什么我不能在我的html模板上获得下拉列表?

Django 为什么我不能在我的html模板上获得下拉列表?,django,django-models,django-forms,django-templates,django-views,Django,Django Models,Django Forms,Django Templates,Django Views,我添加了一个html模板在浏览器中的外观截图。我的表格里没有名字标签的下拉列表。我在模型中获得了所需的输出(下拉列表)。我已经查阅了Django的官方文档,但未能找到解决方案。我是Django的新手,我该怎么办 models.py views.py forms.py url.py home.html .表格容器{ 边界半径:10px; 填充:10px; 盒影:0px 0px 10px 0px; } .行{ 填充:80px; } {%csrf_令牌%} 名称 {{form.name} 数量 {{

我添加了一个html模板在浏览器中的外观截图。我的表格里没有名字标签的下拉列表。我在模型中获得了所需的输出(下拉列表)。我已经查阅了Django的官方文档,但未能找到解决方案。我是Django的新手,我该怎么办

models.py views.py forms.py url.py home.html

.表格容器{
边界半径:10px;
填充:10px;
盒影:0px 0px 10px 0px;
}
.行{
填充:80px;
}
{%csrf_令牌%}
名称
{{form.name}
数量
{{form.amount}}
邮递
输出

使用“从常规视图创建视图”代替“表单视图”,无需再次定义表单中的字段,只需显示模型=记录即可。遵循官方链接:在views.py中,我用CreateView替换了每个FormView,在类home中,分配的model=Record,但没有任何更改。使用通用视图中的create view代替form view,无需再次定义表单中的字段,只需显示model=Record。遵循官方链接:在views.py中,我用CreateView替换了每个FormView,在home类中,分配了model=Record,但没有任何更改。
from django.db import models

class Record(models.Model):
    CHOICES = (  
    ('john', 'JOHN'),
    ('sam', 'SAM'),
    ('lucy', 'LUCY')
)
    date = models.DateTimeField(auto_now_add = True)
    emp_name = models.CharField(max_length=10, choices=CHOICES)
    amount = models.PositiveIntegerField(default=0)
from django.http import HttpResponse
from django.views.generic import FormView
from .models import Record
from .forms import RecordForm

class home(FormView):
    template_name = 'myapp/home.html'
    form_class = RecordForm

    def form_valid(self, form):
        return HttpResponse("Sweeeeeet.")
from django import forms
from .models import Record

CHOICES = [  
    ('john', 'JOHN'),
    ('sam', 'SAM'),
    ('lucy', 'LUCY')
]

class RecordForm(forms.ModelForm):
    name = forms.CharField(label='Name', widget=forms.Select(choices=CHOICES))
    amount = forms.IntegerField()

    class Meta:
        model = Record
        fields = '__all__'
from django.urls import path
from django.contrib.auth import views as auth_views
from .views import EmployeeListView, home
from . import views

urlpatterns = [
    path('', home.as_view(),name='home'),
    path('logout/', auth_views.LogoutView.as_view(template_name = 'myapp/logout.html'), name='home-logout'),
    path('profile/', views.profile, name='home-profile'),
    path('employee/', EmployeeListView.as_view(), name='home-employee'),
]
<style type="text/css">
  .form-container {
      border-radius: 10px;
      padding: 10px;
      box-shadow: 0px 0px 10px 0px;
  }
  .row {
    padding: 80px;
  }
</style>

<div class="row">
  <div class="col s12 m12">
    <div class="card teal lighten-1">
      <div class="card-content white-text">
        <form method="POST" class="form-container">
          {% csrf_token %}
          <div class="fieldWrapper">
            <label for="id_name">Name</label>
            {{ form.name }}
          </div>
          <div class="fieldWrapper">
            <label for="id_amount">Amount</label>
            {{ form.amount }}
        </div>
          <button class="waves-effect waves-light btn" type="submit">Post</button>
      </form>
      </div>
    </div>
  </div>
</div>