Pergunta

I'm new in django and i'm building a site testing the FileField

For now I can upload the files, search, and get the url of files but when i do click in the weblink it returns 'Page not found (404)'

My code is:

In 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})

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

My urls.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),

and finally the media settings

MEDIA_URL = 'http://localhost:8000/'
STATIC_ROOT = '/home/zanklord/Dropbox/django/taqwim/
Foi útil?

Solução

Add this url to urls.py

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

Also add MEDIA_ROOT to settings file. This MEDIA_ROOT will contain the absolute address of the directory in which your static files are saved.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top