用于从django中的数据库删除文件的Typeerror

时间:2021-06-02 20:19:18

I'm trying to get together code that uploads and deletes photos from a django/bootstrap carousel, as well as a database. However, I can't seem to get past this error:

我正在尝试将从django / bootstrap轮播上传和删除照片的代码以及数据库聚集在一起。但是,我似乎无法通过此错误:

TypeError at /alzheimers/delete

delete() takes exactly 2 arguments (1 given)

Can anyone help me? I'm kind of a noob at django, and writing this code is like pulling teeth, so any help would be greatly appreciated.

谁能帮我?我是django的一个菜鸟,写这个代码就像拔牙一样,所以任何帮助都会非常感激。

My code: Carousel.html:

我的代码:Carousel.html:

{% load staticfiles %}
{% load filename %}

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div id="myCarousel" class="carousel slide" data-ride="carousel">


                <div class="carousel-inner" role="listbox">
                    {% for document in documents %}
 <div class="item {% if forloop.first %} active {% endif %}"> 
    <div class="row">
      <div class="col">
        <li><a href = "{{document.docfile.url}}">{{document.docfile.name}}</a></li>
        <img src = "{{STATIC_URL}}img/{{document|filename}}" >
<p align="center"><form style="text-align:center" action="{% url 'webportal:delete' %}" method="post" enctype="multipart/form-data">
            {% csrf_token %}
<p>{{ form.non_field_errors }}</p>
            <p>{{ form.Document.label_tag }} {{ form.Document.help_text }}</p>
            <p>
                {{ form.Document.errors }}
                {{ form.Document.docfile }}
            </p> 
            <p><input type="submit" value="Delete" /></p>
        </form></p>
      </div>
    </div>
  </div>
  {% endfor %}
                </div>
                <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right"></span>
                    <span class="sr-only">Next</span>
                </a>
            </div>
            <!-- /.carousel -->
        </div>
    </div>
<form action="{% url 'webportal:carousel' %}" 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>

</div>

Views.py:

Views.py:

from django.shortcuts import render
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth import authenticate, login
from webportal.views.authentication import LoginForm
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.core.urlresolvers import reverse
from django.conf import settings
from webportal.forms.forms import DocumentForm
from webportal.models import Document, DeleteForm
is_server = True
def delete(request, my_id):
    Deleted=get_object_or_404(Document, docfile=my_id)
    if request.method=='POST':
        form=DeleteForm(request.POST, instance=Deleted)
        if form.is_valid():
            Deleted.delete()
            return HttpResponseRedirect('http://127.0.0.1:8000/alzheimers/')
    else:
        form=DeleteForm(instance=Deleted)
    return render_to_response(
        'webportal/index.html',
        {'documents': documents, 'form': form,},
        context_instance=RequestContext(request)
    )        

            # Redirect to the document list after POST
def carousel(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect('http://127.0.0.1:8000/alzheimers/')



else:



 form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
#documents=DocumentForm().
# Render list page with the documents and the form
return render_to_response(
    'webportal/index.html',
    {'documents': documents, 'form': form,},
    context_instance=RequestContext(request)
)

Models.py:

Models.py:

class Document(models.Model):
    docfile = models.ImageField(upload_to='webportal/static/img/')
class DeleteForm(ModelForm):
    class Meta:
        model=Document
        fields=[]

Forms.py:

Forms.py:

class DocumentForm(forms.Form):
    docfile = forms.ImageField(label='Select a file', help_text='max. 42 megabytes')

urls.py:

urls.py:

url(r'^delete', views.delete, name="delete"),

url(r'^ delete',views.delete,name =“delete”),

1 个解决方案

#1


1  

You are POSTing your object id through a form while your delete view expects it as an argument (my_id). Change it to:

您正在通过表单发布对象ID,而删除视图则将其作为参数(my_id)。将其更改为:

def delete(request):
    if request.method=='POST':
        my_id = request.POST.get('id')
        Deleted=get_object_or_404(Document, id=my_id)
        ...

As a side note, the Python convention is to use lowercase names for objects. Consider renaming Deleted to deleted.

作为旁注,Python约定是对对象使用小写名称。考虑将已删除重命名为已删除。

Update: You also seem to have omitted to include the id of the object to be deleted in your form. Put the following line somewhere between your <form> and </form> tags:

更新:您似乎也省略了在表单中包含要删除的对象的ID。将以下行放在

和 标记之间的某处:

<input type="hidden" name="id" value="{{ document.id }}" />

#1


1  

You are POSTing your object id through a form while your delete view expects it as an argument (my_id). Change it to:

您正在通过表单发布对象ID,而删除视图则将其作为参数(my_id)。将其更改为:

def delete(request):
    if request.method=='POST':
        my_id = request.POST.get('id')
        Deleted=get_object_or_404(Document, id=my_id)
        ...

As a side note, the Python convention is to use lowercase names for objects. Consider renaming Deleted to deleted.

作为旁注,Python约定是对对象使用小写名称。考虑将已删除重命名为已删除。

Update: You also seem to have omitted to include the id of the object to be deleted in your form. Put the following line somewhere between your <form> and </form> tags:

更新:您似乎也省略了在表单中包含要删除的对象的ID。将以下行放在

和 标记之间的某处:

<input type="hidden" name="id" value="{{ document.id }}" />