当我运行以下程序时,出现错误:AttributeError:'CreateBatchForm'对象没有属性'get'
我试图更改对象的名称并更新了传递给“ create_batch_admin”形式的参数,但这无济于事。
urls.py
urlpatterns += [
#path('batches/create/', views.CreateBatchForm, name='create_batch_admin'),
path('batches/create/', views.CreateBatchForm, name='create_batch_admin'),
]
views.py
import datetime
from django.contrib.auth.decorators import permission_required
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
from django.urls import reverse
from catalog.forms import CreateBatchForm
#@permission_required('catalog.can_mark_returned')
def create_batch_admin(request):
"""View function for creating a Batch by admin."""
template_name = 'catalog/create_batch_admin.html'
# If this is a POST request then process the Form data
if request.method == 'POST':
print("Hello")# Create a form instance and populate it with data from the request (binding):
form = CreateBatchForm(request.POST or None)
errors = None
# Check if the form is valid:
if form.is_valid():
Batch.objects.create(
batch_Name = form.cleaned_data.get('batch_Name'),
start_Date = form.cleaned_data.get('start_Date'),
end_Date = form.cleaned_data.get('end_Date'),
time = form.cleaned_data.get('time'),
)
# redirect to a new URL:
#return HttpResponseRedirect(reverse('index') )
return HttpResponseRedirect("/s/")
if form.errors:
errors = form.errors
context = {
'form': form,
'num_Batches': num_Batches,
"errors": errors
}
return render(request, template_name, context)
else:
form = CreateBatchForm()
if 'submitted' in request.GET:
submitted = True
return render(request, template_name, {'form':form})
create_batch_admin.html
{% extends "base_generic.html" %}
{% block content %}
<h1>Create: {{ batch.batch_Name }}</h1>
<p>Start Date: {{ batch.start_Date }}</p>
<p>End Date: {{ batch.start_Date }}</p>
<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
{% endblock %}
models.py
...
class Batch(models.Model):
"""Model representing a Batch."""
course = models.ForeignKey('Course', on_delete=models.SET_NULL, null=True)
batch_Name = models.CharField(max_length=50, help_text='Enter Batch Name', null=True)
start_Date = models.DateTimeField (help_text='Enter Batch Date and Time', null=True)
end_Date = models.DateTimeField (help_text='Enter Batch Date and Time', null=True)
time = models.TimeField (help_text='Enter Time of the day when the batch runs', null=True)
BATCH_STATUS = (
('P', 'Planned'),
('A', 'Active'),
('I', 'Inactive'),
)
status = models.CharField(
max_length=1,
choices=BATCH_STATUS,
blank=True,
default='',
help_text='Enter Batch Status',
)
description = models.TextField(max_length=200, help_text='More Details about the course', null=True)
def get_absolute_url(self):
"""Returns the url to access a particular student instance."""
return reverse('batch-detail', args=[str(self.id)])
def __str__(self):
"""String for representing the Model object."""
return self.batch_Name
forms.py
from django import forms
class CreateBatchForm(forms.Form):
batch_Name = forms.CharField(help_text="Enter the Name of the batch.")
start_Date = forms.DateField(help_text="Enter the Start date for the batch.")
end_Date = forms.DateField(help_text="Enter the End date for the batch.")
time = forms.DateTimeField(help_text="Enter the time of the day when the batch runs.")
答案 0 :(得分:1)
朋友,我刚刚发现我需要更改文件 urls.py 从 这个
path('batches/create/', views.CreateBatchForm, name='create_batch_admin'),
对此
path('batches/create/', views.create_batch_admin, name='create_batch_admin'),