什么是执行保存的最佳方式,因为目前。在进行编辑时,我没有收到保存的回复来填充表单。其他字段(例如下拉菜单)很好。为了使这项工作,我应该做些什么?这是我的看法:
def populateaboutme(request):
extractlinkedindata(request)
if request.method == "POST":
form = AboutMeForm(request.POST)
if form.is_valid():
today = datetime.date.today()
currentYYMMDD = today.strftime('%Y-%m-%d')
model_instance = form.save(commit=False)
model_instance.save()
request.session["AboutMe_id"] = model_instance.pk
StoreImage(settings.STATIC_ROOT, str(request.session["fotoloc"]), '.jpg', str(request.session["AboutMe_id"]))
return redirect('/dashboard/')
else:
myid = request.session["AboutMe_id"]
if not myid:
form = AboutMeForm()
else:
aboutme = AboutMe.objects.get(pk=int(myid))
form = AboutMeForm(instance=aboutme)
return render(request, "aboutme.html", {'form': form})
Here are the models:
class AboutMe(models.Model):
MyRelationshipIntent = models.CharField(max_length=50)
和表格:
class AboutMeForm(ModelForm):
class Meta:
model = AboutMe
exclude = ()
MyRelationshipIntent = forms.MultipleChoiceField(choices=RELATIONSHIPINTENT_CHOICES,widget=forms.CheckboxSelectMultiple())
RELATIONSHIPINTENT_CHOICES = (
('JL', 'Just Looking'),
('FL', 'Looking for friendship'),
('FN', 'Looking for fun'),
('FL', 'Looking for a relationship'),
)