简体   繁体   English

上载文件,Django中的网址出现问题

[英]Upload a file, having problems with the url in Django

I'm doing a function to upload a file with a example that I found here on Stack Overflow, everything works fine, the problem is that when I try to access the document by a URL, the click sends me to the app url not the photo url, for example I'm trying to access to this 我正在使用上面在Stack Overflow上找到的示例执行上传文件的功能,一切正常,问题是当我尝试通过URL访问文档时,单击会将我发送到应用程序URL,而不是照片网址,例如我正在尝试访问该网址

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

and it should be the image URL 它应该是图片网址

documents/2013/10/01/Desert.jpg

view.py 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 Setting.py

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

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

You are accessing a model not an image. 您正在访问模型而不是图像。 Also, this might help: 此外,这可能会有所帮助:

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

Please take a look there as i don't see this added to the urls.py you pasted. 请在那里查看,因为我没有看到此内容添加到您粘贴的urls.py中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM