분명히 뭔가를 놓치고있는 것처럼 느껴지지만 내 모델 formsets가 제출 후에 데이터를 유지해야하는 문제가 있습니다. 나는 사용자가 프로젝트를 생성하고 그 프로젝트에 임의의 양의 머티리얼을 추가 할 수있는 페이지를 생성하고 있습니다. JavaScript는 필요에 따라 formset의 새 인스턴스를 동적으로 추가 처리합니다. 코드는 처음부터 잘 작동하고, 그 후에는 이전 데이터를 "기억"합니다. 재질 formset에서는 발생하지만 그 위에있는 일반 모델 양식에서는 발생하지 않습니다.Django Model Formset 데이터 항상 유지
내 모델 formset을 만드는 방식과 관련이 있다고 생각합니다. 페이지가 요청되면보기 바인딩되지 않은 집합이 아닌 이전 데이터 바인딩 된 formset 다시 전달하는 것 같습니다. 나는 장고를 처음 사용하고 있으며 스스로 가르치려고 노력하고 있기 때문에 아마도 아직 이해하지 못하는 일들이있을 것입니다. 그것도 도움이 경우 내 템플릿 코드에 추가 할 수
def addproject_page(request):
# Define the formset to use to add the materials
MaterialFormSet = modelformset_factory(Material, exclude = ('project',))
# Check to see if there is some POST data from an attempt to fill out the form
if request.method == 'POST':
# Create a form for the project and for the material and use a prefix to separate the POST data for the two
project_form = ProjectForm(request.POST, prefix='project')
# Instantiate the formset to display multiple materials when creating a project
material_formset = MaterialFormSet(request.POST, prefix='material')
# Check both forms with the validators and if both are good process the data
if project_form.is_valid() and material_formset.is_valid():
# Save the data for the newly created project
created_project = project_form.save()
# Tell each material to be associated with the above created project
instances = material_formset.save(commit=False)
for instance in instances:
instance.project = created_project
instance.save()
# After the new project and its materials are created, go back to the main project page
return HttpResponseRedirect('/members/projects/')
# If there is no post data, create and show the blank forms
else:
project_form = ProjectForm(prefix='project')
material_formset = MaterialFormSet(prefix='material')
return render(request, 'goaltracker/addproject.html', {
'project_form': project_form,
'material_formset': material_formset,
})
편집 :
{% extends "base.html" %}
{% block external %}
<script src="{{ static_path }}js/projects.js" type="text/javascript"></script>
{% endblock %}
{% block title %}: Add Project{% endblock %}
{% block content %}
<h1>Add a Project</h1>
<form id="new_project_form" method="post" action="">
{{ project_form.as_p }}
<!-- The management form must be rendered first when iterating manually -->
<div>{{ material_formset.management_form }}</div>
<!-- Show the initial blank form(s) before offering the option to add more via JavaScript -->
{% for material_form in material_formset.forms %}
<div>{{ material_form.as_p }}</div>
{% endfor %}
<input type="button" value="Add Material" id="add_material">
<input type="button" value="Remove Material" id="remove_material">
<input type="submit" value="add" />
</form>
{% endblock %}
논의되었다. 이 문제가 생겼을 때 당신은 무엇을하고 있습니까? 페이지를 어떻게 다시 표시하고 있습니까? 브라우저가 데이터를 캐싱 할 수 있습니까? –
브라우저 캐시가 될 수 있다고 생각했습니다. 캐시와 뚜껑을 닫고 완전히 다른 브라우저에서 페이지로 이동하려고했지만 다른 브라우저에서도 데이터가 표시되었습니다. 개발 서버를 중지하고 시작한 후에도 유지됩니다. "재설정"할 수있는 유일한 방법은 세션 및 관련 테이블을 삭제하고 syncdb를 다시 실행하는 것입니다. –