2

我是 django 的新手,我正在构建一个测试 FileField 的站点

现在我可以上传文件、搜索和获取文件的 url,但是当我点击网络链接时,它返回“找不到页面 (404)”

我的代码是:

在views.py中:

from django.shortcuts import render, get_object_or_404
from granada.models import Hojas

def index(request):
    return render(request, 'index.html')

def search_form(request):
    return render(request, 'search_form.html')

def search(request):
    errors = []
    if 'field1' in request.GET:
        field1 = request.GET['field1']
        field2 = request.GET['field2']
        if not (field1 or field2):
            errors.append('Enter a search term.')
        else:
            results = Hojas.objects.filter(
                mes__icontains=field1
            ).filter(
                agno__icontains=field2
            )
            query = "%s, %s" % (field1, field2)

            return render(request, 'search_results.html',
                    {'results': results, 'query': query})
    return render(request, 'search_form.html',
            {'errors': errors})

和我的 search_results.html

<p>Buscó la hoja del mes y año: <strong>{{ query }}</strong></p>

{%for result in results%} 

    <div class="post">
        <ul>
            <h3>
                <a href="{{ result.Granada.url }}">
                    {{result}}
                </a>
            </h3>
        </ul>

{%endfor %}

我的网址.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
from granada import views
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    (r'^search-form/$', views.search_form),
    (r'^search/$', views.search),

最后是媒体设置

MEDIA_URL = 'http://localhost:8000/'
STATIC_ROOT = '/home/zanklord/Dropbox/django/taqwim/
4

1 回答 1

2

将此网址添加到 urls.py

url(r'^media/(?P<path>.*)$','django.views.static.serve', {'document_root':settings.MEDIA_ROOT,'show_indexes':True}),

还将 MEDIA_ROOT 添加到设置文件中。此 MEDIA_ROOT 将包含保存静态文件的目录的绝对地址。

于 2013-07-22T19:35:54.527 回答