【发布时间】:2018-09-02 10:01:46
【问题描述】:
我不断收到错误消息,不知道为什么。例如,我认为这与变量有关,但我在互联网上看到很多以相同方式工作的示例。
models.py
class Establishments(models.Model):
title = models.CharField(max_length=255)
town = models.ForeignKey(Town, on_delete=SET_NULL, null=True)
addrstreet = models.CharField(max_length=255)
addrzip = models.CharField(max_length=12)
telephone = models.CharField(max_length=15)
email = models.CharField(max_length=255)
chamberofcomnr = models.CharField(max_length=25)
description = models.TextField(max_length=255)
website = models.CharField(max_length=255)
categorie = models.ForeignKey(Establishmentcategory, on_delete=SET_NULL, null=True)
pub_date = models.DateTimeField('date published')
drupuser = models.ForeignKey(Drupalusers, on_delete=SET_NULL, null=True)
druppublished = models.BooleanField()
drupurl = models.CharField(max_length=255)
drupnodeid = models.IntegerField()
def __str__(self):
return self.title
class Impcalendar(models.Model):
establishment = models.ForeignKey(Establishments, on_delete=SET_NULL, null=True)
active = models.BooleanField()
prio = models.IntegerField()
url = models.CharField(max_length=255)
check_intervalh = models.IntegerField()
check_fixedh = models.IntegerField()
log = models.BooleanField()
cuttag = models.CharField(max_length=255)
cuttxt = models.CharField(max_length=255)
cuttxtend = models.CharField(max_length=255)
comment = models.CharField(max_length=255)
page = models.TextField()
pageold = models.TextField()
change = models.TextField()
pagedate = models.DateTimeField()
pagedatenext = models.DateTimeField()
status = models.IntegerField()
errors = models.IntegerField()
def __str__(self):
return str(self.id)
urls.py
path('calendar/<int:calendar_id>/', views.calendaredit, name='calendaredit')
views.py
def calendaredit(request, calendar_id):
calendar = get_object_or_404(Impcalendar, pk=calendar_id)
print (calendar.url)
form = ImpcalendarForm(request.POST or None, instance=calendar)
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
print (form.url)
# check whether it's valid:
if form.is_valid():
#calendar.establishment = form.cleaned_data['
calendar = form.save(commit=false)
calendar.active = form.cleaned_data['active']
calendar.save()
return redirect('handmatig')
return render(request, 'import_calendar/handmatig_edit.html', {'form': form})
forms.py
class ImpcalendarForm(forms.Form):
establishment = forms.ModelChoiceField(queryset = Establishments.objects.all())
page = forms.CharField(widget=forms.Textarea)
pageold = forms.CharField(widget=forms.Textarea)
change = forms.CharField(widget=forms.Textarea)
class Meta:
model = Impcalendar
fields = '__all__'
所以我想要一个记录页面,列出所有已经工作的记录,我可以在其中编辑表格。它需要将记录显示为 Django 表单。它在线上崩溃; form = ImpcalendarForm(request.POST or None, instance=calendar)
如果我打印变量 calendar 或 calendar.url 我得到正确的数据。 错误信息是;
TypeError: __init__() got an unexpected keyword argument 'instance'
花一周时间调试。现在升级了。 ;-)
【问题讨论】:
标签: django python-3.x django-forms