سؤال

sorry my question is very basic ,I want to display an image in template page,I am working hours for it,The image is not displaying I create a new project SecondPrjt ,there is only one view function named 'index' and one template named test.html , and create a folder named static in side SecondPrjt folder and create folder images in it and place all needed images there
urls.py:

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'SecondPrjt.views.home', name='home'),
(r'^$','views.index'),
......... )

urlpatterns += staticfiles_urlpatterns()

views.py from django.shortcuts import render_to_response
def index(request):
return render_to_response('test.html')


test.html:
img src="{{ STATIC_URL }}images/img03.jpg" width="186" height="186"

settings.py


STATIC_ROOT = "C:/wamp/www/SecondPrjt/static"
STATIC_URL = '/static/'
STATICFILES_DIRS = ( "C:/wamp/www/SecondPrjt/static", )
STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder',

)
INSTALLED_APPS = (
-----
'django.contrib.staticfiles',
---- )

Can any one suggest whats wrong with me.NoteI am using windows and django1.3 and using development server.Thanks in advance
I got the following after I request http://127.0.0.1:8000/ in browse
c:\wamp\www\SecondPrjt>python manage.py runserver Validating models...

0 errors found Django version 1.3, using settings 'SecondPrjt.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. [05/Sep/2011 15:18:24] "GET / HTTP/1.1" 200 89 [05/Sep/2011 15:18:24] "GET /images/img03.jpg HTTP/1.1" 404 2028

هل كانت مفيدة؟

المحلول

In all likelihood, your problem is that you've used absolute URLs in both the STATIC_ROOT and STATICFILES_DIRS.

If you want to include absolute URLs to each static dir (it feels like a hack, but it is fine, as you should only ever use staticfiles_urlpatterns() in development), then you can fix the issue by simply setting STATIC_ROOT = ''.

Edit: Looks like that wasn't your only bug. In addition, if you take a look at your debug output, you'll notice something strange about the 404 -- it's coming from /images/img03.jpg. If you'll remember, you tried to append {{ STATIC_URL }} to that, so it should have been /static/images/img03.jpg.

This is because when using render_to_response, you never pass any context (so the template never has access to STATIC_URL -- it thinks it's blank (hence why you didn't see the /static/ as part of the image URL. Whenever you want to use ANY context, you MUST pass it to the template. In this case, you should use [RequestContext].

Therefore, you must make sure you have added django.core.context_processors.static to TEMPLATE_CONTEXT_PROCESSORS in settings.py. You should use render_to_response as follows:

def index(request):
    return render_to_response('test.html', context_instance=RequestContext(request))


The following is an explanation of what context processors are:

In settings.py, you then would have something like:

 TEMPLATE_CONTEXT_PROCESSORS = (
     'constant_context_processor.constants',
     'django.core.context_processors.static',
     'django.contrib.auth.context_processors.auth',
     'django.core.context_processors.debug',
     'django.core.context_processors.i18n',
     'django.core.context_processors.media',
     'django.core.context_processors.request', )

The important part here is that you have django.core.context_processors.static. In Django, a context processor is a function that returns a context dict. For instance, in my Django projects, I often use the following code snippet:

constant_context_processor.py:

import settings
def constants(request):
    return {
           'CONSTANTS': settings.CONSTANTS,
    }

(You'll note that above in settings.py, I used constant_context_processor.constants as one of the context processors). This allows me to define constants in settings.py and to use them in my templates, for instance as something like {{ CONSTANTS.favicon_url }}.

As you can see, the advantage to context processors and using RequestContext is that context you generally need in your templates gets automatically added, so you don't have to manually populate a dictionary of your context in every view. Django knows there are certain variables you usually want access to in your templates, so they make them accessible via their context processors (so, for instance, django.core.context_processors.request will give you access to a given request, its GET and POST parameters, and all kinds of metadata from a template, which is often useful if, say, you vary the content based on the GET parameters).

In your case, you didn't want to use much context, but you did want to use a little bit ({{ STATIC_URL }}), so you needed at least django.core.context_processors.static.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top