0

我正在做一个上传文件的功能,其中包含我在 Stack Overflow 上找到的示例,一切正常,问题是当我尝试通过 URL 访问文档时,点击将我发送到应用程序 url 而不是照片网址,例如我正在尝试访问此

http://localhost:8000/media/documents/2013/10/01/Desert.jpg

它应该是图片网址

documents/2013/10/01/Desert.jpg

视图.py

def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        print form
        if form.is_valid():
            newdoc = Document(docfile=request.FILES['docfile'],credencial_miembro=request.POST['credencial_miembro'])
            print newdoc
            newdoc.save()
            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('expmedico.views.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_to_response(
        'upload.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )

表格.py

class DocumentForm(forms.Form):
    docfile = forms.FileField(
        label='Select a file',
        help_text='max. 42 megabytes'
    )
    credencial_miembro=forms.CharField(max_length=20)

模型.py

class Document(models.Model):
    docfile = models.FileField(upload_to='documents')
    credencial_miembro= models.CharField(max_length=20,null=False, blank=False)

超尔

 url(r'^list/$', 'expmedico.views.list',name='list'),

设置.py

MEDIA_ROOT = os.path.join(RUTA_PROYECTO, 'media')
MEDIA_URL = '/media/'

模板.html

<!DOCTYPE html>
 <html>
<head>
    <meta charset="utf-8">
    <title>Minimal Django File Upload Example</title>   
</head>

<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 %}


    <table style="width:150%;">
        <div style="text-align:center; font-style: oblique; color: red" >
     {% if messages %}
    <ul class="messages">
        {% for message in messages %}
            <li{% if message.tags %} class="{{ message.tags }}"{% endif %}><b>{{   message  }}</b></li>
        {% endfor %}
    </ul>
  {% endif %}
  </div>

        <tr>
            <td><label>Fecha:</label></td>
            <td>
                <output><b>{% now "D d M Y" %}</b></output>
            </td>
            <td>{{ form.credencial_miembro.errors }}<label>Credencial:</label></td>
            <td>{{ form.credencial_miembro }} </td>

         </tr>
     </table>
        <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>
 </html>
4

1 回答 1

1

您正在访问模型而不是图像。此外,这可能会有所帮助:

https://docs.djangoproject.com/en/1.2/howto/static-files/

请看那里,因为我没有看到它添加到您粘贴的 urls.py 中。

于 2013-10-01T17:55:46.750 回答