上传一个文件,Django中的url有问题

时间:2013-10-01 16:36:42

标签: python django

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

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

它应该是图片网址

documents/2013/10/01/Desert.jpg

view.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)
    )

forms.py

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

model.py

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

ULR

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

Setting.py

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

template.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>

1 个答案:

答案 0 :(得分:1)

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

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

请看一下,因为我没有看到这个添加到您粘贴的urls.py.