Python 从Django中的数据库中删除元素
我正在尝试开发一个Django应用程序,能够对文件进行CRUD。 目前,我已经开发了上传下载功能,但当我想删除项目时,事情变得非常困难 *Views.py*Python 从Django中的数据库中删除元素,python,django,django-2.0,Python,Django,Django 2.0,我正在尝试开发一个Django应用程序,能够对文件进行CRUD。 目前,我已经开发了上传下载功能,但当我想删除项目时,事情变得非常困难 *Views.py* def list(request): # Handle file upload if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) all_docs = Document.objects.all() repeate
def list(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
all_docs = Document.objects.all()
repeated = False
if form.is_valid():
for doc in all_docs:
if Document(docfile=request.FILES['docfile']).docfile == doc.docfile:
repeated = True
if not repeated:
newdoc = Document(docfile=request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('list'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render(
request,
'list.html',
{'documents': documents, 'form': form}
)
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='Select a file'
)
<body>
<!-- List of uploaded documents -->
{% if documents %}
<ul>
{% for document in documents %}
<li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No documents.</p>
{% endif %}
<!-- Upload form. Note enctype attribute! -->
<form action="{% url "list" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload"/></p>
</form>
</body>
def remove_document(request):
if request.method == 'POST':
form = DocumentForm()
# Dont know how to reference the item I want to delete
document = request.POST.objects.get(docfile = ?????)
db_documents = Document.objects.all()
for db_doc in db_documents:
if db_doc = document:
document.delete()
return render(
request,
'list.html',
{'documents': documents, 'form': form}
)
*Forms.py*
def list(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
all_docs = Document.objects.all()
repeated = False
if form.is_valid():
for doc in all_docs:
if Document(docfile=request.FILES['docfile']).docfile == doc.docfile:
repeated = True
if not repeated:
newdoc = Document(docfile=request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('list'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render(
request,
'list.html',
{'documents': documents, 'form': form}
)
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='Select a file'
)
<body>
<!-- List of uploaded documents -->
{% if documents %}
<ul>
{% for document in documents %}
<li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No documents.</p>
{% endif %}
<!-- Upload form. Note enctype attribute! -->
<form action="{% url "list" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload"/></p>
</form>
</body>
def remove_document(request):
if request.method == 'POST':
form = DocumentForm()
# Dont know how to reference the item I want to delete
document = request.POST.objects.get(docfile = ?????)
db_documents = Document.objects.all()
for db_doc in db_documents:
if db_doc = document:
document.delete()
return render(
request,
'list.html',
{'documents': documents, 'form': form}
)
*List.html*
def list(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
all_docs = Document.objects.all()
repeated = False
if form.is_valid():
for doc in all_docs:
if Document(docfile=request.FILES['docfile']).docfile == doc.docfile:
repeated = True
if not repeated:
newdoc = Document(docfile=request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('list'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render(
request,
'list.html',
{'documents': documents, 'form': form}
)
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='Select a file'
)
<body>
<!-- List of uploaded documents -->
{% if documents %}
<ul>
{% for document in documents %}
<li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No documents.</p>
{% endif %}
<!-- Upload form. Note enctype attribute! -->
<form action="{% url "list" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload"/></p>
</form>
</body>
def remove_document(request):
if request.method == 'POST':
form = DocumentForm()
# Dont know how to reference the item I want to delete
document = request.POST.objects.get(docfile = ?????)
db_documents = Document.objects.all()
for db_doc in db_documents:
if db_doc = document:
document.delete()
return render(
request,
'list.html',
{'documents': documents, 'form': form}
)
我是Django的新手,它的架构模型对我来说太棘手了
谢谢 不幸的是,这对于一个正确的答案来说实在太宽泛了。您的更新代码非常混乱,这导致您在编写删除代码时误入歧途。作为提示,您不应该尝试使用这两种方法中的任何一种迭代所有文档;您应该通过
.objects.get()
直接获取与接收到的ID相对应的实例,然后更新或删除它。我想我会用另一种方式,一种更“常见”的方式。谢谢你的回答!