我有一个 CBV,ListView首先在这里使用我的views.py
class InventoryListView(ListView):
context_object_name = 'inventorys'
model = models.Inventory
Run Code Online (Sandbox Code Playgroud)
这里是我的 template_list.html
{% for inventory in inventorys %}
<tr>
<td>{{ inventory.name }}</td>
<td>{{ inventory.sn }}</td>
<td>{{ inventory.employee.name }}</td>
<td>{{ inventory.desc }}</td>
</tr>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
它按预期返回所有数据。
但我需要用它添加表单。然后将一些代码添加到我的views.py
class InventoryListView(ListView):
template_name ='system/inventory_list.html'
context_object_name = 'inventorys'
model = models.Inventory
def get(self, request):
form = InventoryForm()
return render(request, self.template_name, {'form': form})
def post(self, request):
form = InventoryForm(request.POST)
Run Code Online (Sandbox Code Playgroud)
这里是我的 forms.py
class InventoryForm(forms.ModelForm):
name = forms.CharField(max_length=255)
sn = forms.DecimalField(max_digits=20, decimal_places=0)
desc …Run Code Online (Sandbox Code Playgroud) 发布数据时出错
AttributeError at /system/create/
type object 'object' has no attribute 'EmpInstallment'
Request Method: POST
Request URL: http://127.0.0.1:8000/system/create/
Django Version: 1.11.2
Exception Type: AttributeError
Exception Value:
type object 'object' has no attribute 'EmpInstallment'
Exception Location: /Users/wakanda/pyproject/payroll/system/views.py in form_valid, line 117
Python Executable: /Users/wakanda/py-virtualenv/payroll3/bin/python
Python Version: 3.6.5
Python Path:
['/Users/wakanda/pyproject/payroll',
'/Users/wakanda/py-virtualenv/payroll3/lib/python36.zip',
'/Users/wakanda/py-virtualenv/payroll3/lib/python3.6',
'/Users/wakanda/py-virtualenv/payroll3/lib/python3.6/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Users/wakanda/py-virtualenv/payroll3/lib/python3.6/site-packages',
'/Users/wakanda/py-virtualenv/payroll3/lib/python3.6/site-packages/setuptools-39.0.1-py3.6.egg',
'/Users/wakanda/py-virtualenv/payroll3/lib/python3.6/site-packages/configparser-3.5.0-py3.6.egg']
Run Code Online (Sandbox Code Playgroud)
模型
class Employee(models.Model):
nik = models.CharField(max_length=100)
name = models.CharField(max_length=100)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("system:detail",kwargs={'pk':self.pk})
class EmpLoan(models.Model):
status = models.BooleanField()
nominal = models.DecimalField(max_digits=10, …Run Code Online (Sandbox Code Playgroud) 当我在表单中使用引导模式时,它只显示第一个值。
在这里我的template.html
{% for company in companys %}
<tr>
<td>{{ company.name }}</td>
<td>{{ company.desc }}</td>
<td align="center">
<button type="button" class="btn btn-warning margin-bottom" data-toggle="modal" data-target="#modal-default2">
delete
</button>
<div class="modal fade" id="modal-default2">
<div class="modal-dialog">
<form method="post" action="{% url 'system:company_delete' pk=company.pk %}">
{% csrf_token %}
<div class="modal-content">
<div class="modal-body">
<input type="text" name="name" maxlength="100" required="" id="id_name" value="{{ company.pk }}">
<input type="submit" class="btn btn-primary" value="Delete">
</div>
</div>
</form>
</div>
</div>
</td>
</tr>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
它循环所有数据,当点击delete确认表单时会弹出。但它返回相同的值。
但如果没有模态引导它的工作正常。
例子:template.html
{% for company …Run Code Online (Sandbox Code Playgroud) javascript django django-templates django-forms bootstrap-modal
出现错误 Django NameError 名称“请求”未定义。
我的 views.py
class ShiftCreateView(CreateView):
fields = ('name', 'timein', 'timeout', 'desc')
model = models.Shift
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.timein = request.POST.get("timein", "")
self.object.timeout = request.POST.get("timeout", "")
self.object.save()
return super(ModelFormMixin, self).form_valid(form)
Run Code Online (Sandbox Code Playgroud)