Django: 404 page not found

1,080 views
Skip to first unread message

Homer

unread,
Apr 2, 2012, 8:14:00 PM4/2/12
to django...@googlegroups.com
I met "404" page not found when I try to enter 127.0.0.1:8000/cn/bedroom . It says on the webpage that "C:/Django/final/media/bedroom" does not exist". Why would this happen?

urls.py:

from django.conf.urls.defaults import *
from final import settings
 
from django.contrib import admin
 
admin.autodiscover()
 
urlpatterns = patterns('',
 
    url(r'^admin/', include(admin.site.urls)),
 
    url(r'^cn/', include('final.photo.urls')),
 
    url(r'^cn/(?P<path>.*)$', 'django.views.static.serve',
 
        {'document_root': settings.MEDIA_ROOT}),
 
)
photo/urls.py:

from django.conf.urls.defaults import *
 
from final.photo.views import List, Detail
 
urlpatterns = patterns('',
 
    url(r'^$', List),
    url(r'^/bedroom/', Detail),
 
)
 
photo/views.py:

 
from django.template import loader, Context, RequestContext
 
from django.http import HttpResponse
 
from final.photo.models import Image, Audio, Pinyin, SImage
 
from django.shortcuts import render_to_response
 
def List(request):
 
    ShowSImage = SImage.objects.all()
    ShowLink = Image.objects.all()
 
    context = RequestContext(request, {
 
         'ShowSImage': ShowSImage, 'ShowLink': ShowLink
 
    })
 
    return render_to_response('list.html', context)
 
def Detail(request):
    ShowImage = Image.objects.all()
    ShowPinyin = Pinyin.objects.all()
    ShowAudio = Audio.objects.all()
    context = RequestContext(request, {
        'ShowAudio': ShowAudio, 'ShowImage': ShowImage, 'ShowPinyin': ShowPinyin
    })
    return render_to_response('detail.html', context)
detail.html:

{% extends "base.html" %}
 
{% block title %}{{ item.title }}{% endblock %}
 
{% block content %}
 
<h2>{{ item.title }}</h2>
<img src="{{ item.get_image_url }}" />
{% if object.caption %}<p>{{ object.caption }}</p>{% endif %}
 
{% endblock %}
Thanks in advance!

Stanwin Siow

unread,
Apr 2, 2012, 8:41:02 PM4/2/12
to django...@googlegroups.com
check your Media_root in settings.py.

It could be a missing stroke.

Best Regards,

Stanwin Siow



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/V1uKDrAvYS0J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Homer

unread,
Apr 2, 2012, 9:20:21 PM4/2/12
to django...@googlegroups.com
I think "media_root" works well since I also used pictures on my another page. Maybe there is something wrong in my urlconf...

在 2012年4月2日星期一UTC-4下午8时41分02秒,St@n写道:

Best Regards,

Stanwin Siow



To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.

Sergiy Khohlov

unread,
Apr 3, 2012, 2:20:30 AM4/3/12
to django...@googlegroups.com
please post section of the setting.py file

TEMPLATE_CONTEXT_PROCESSORS

Has this file

'django.core.context_processors.static',

2012/4/3 Homer <hi...@foxmail.com>:

>> django-users...@googlegroups.com.


>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>>

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/django-users/-/Os90mGtKgrIJ.


>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to

> django-users...@googlegroups.com.

Homer

unread,
Apr 3, 2012, 8:53:34 AM4/3/12
to django...@googlegroups.com
Sorry, I did not find "TEMPLATE_CONTEXT_PROCESSORS" in my settings.py. I do not know whether it is related to the version of Django. I am using Django 1.3 right now.

在 2012年4月3日星期二UTC-4上午2时20分30秒,skhohlov写道:

>> django-users+unsubscribe@googlegroups.com.


>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/Os90mGtKgrIJ.
>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to

> django-users+unsubscribe@googlegroups.com.

Message has been deleted

Sergiy Khohlov

unread,
Apr 3, 2012, 8:57:57 AM4/3/12
to django...@googlegroups.com
this section from my last project :


TEMPLATE_CONTEXT_PROCESSORS = (

'django.core.context_processors.request',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)

You have problem without 'django.core.context_processors.media'
with media content , take a look at
https://docs.djangoproject.com/en/dev/howto/static-files/


2012/4/3 Homer <hi...@foxmail.com>:

>> >> django-users...@googlegroups.com.


>> >> For more options, visit this group at
>> >> http://groups.google.com/group/django-users?hl=en.
>> >>
>> >>
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Django users" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/django-users/-/Os90mGtKgrIJ.
>> >
>> > To post to this group, send email to django...@googlegroups.com.
>> > To unsubscribe from this group, send email to

>> > django-users...@googlegroups.com.


>> > For more options, visit this group at
>> > http://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/django-users/-/70nj8U1BxCoJ.


>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to

> django-users...@googlegroups.com.

Homer

unread,
Apr 3, 2012, 9:49:26 AM4/3/12
to django...@googlegroups.com
I add the section you provided to my settings.py but it still does not work... 

在 2012年4月3日星期二UTC-4上午8时57分57秒,skhohlov写道:

>> >> django-users+unsubscribe@googlegroups.com.


>> >> For more options, visit this group at
>> >> http://groups.google.com/group/django-users?hl=en.
>> >>
>> >>
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Django users" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/django-users/-/Os90mGtKgrIJ.
>> >
>> > To post to this group, send email to django...@googlegroups.com.
>> > To unsubscribe from this group, send email to

>> > django-users+unsubscribe@googlegroups.com.


>> > For more options, visit this group at
>> > http://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/70nj8U1BxCoJ.
>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to

> django-users+unsubscribe@googlegroups.com.

Sergiy Khohlov

unread,
Apr 3, 2012, 9:58:54 AM4/3/12
to django...@googlegroups.com
Could you please add your setting.py from root of the your project to
your letter ?

2012/4/3 Homer <hi...@foxmail.com>:

>> >> >> django-users...@googlegroups.com.


>> >> >> For more options, visit this group at
>> >> >> http://groups.google.com/group/django-users?hl=en.
>> >> >>
>> >> >>
>> >> > --
>> >> > You received this message because you are subscribed to the Google
>> >> > Groups
>> >> > "Django users" group.
>> >> > To view this discussion on the web visit
>> >> > https://groups.google.com/d/msg/django-users/-/Os90mGtKgrIJ.
>> >> >
>> >> > To post to this group, send email to django...@googlegroups.com.
>> >> > To unsubscribe from this group, send email to

>> >> > django-users...@googlegroups.com.


>> >> > For more options, visit this group at
>> >> > http://groups.google.com/group/django-users?hl=en.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Django users" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/django-users/-/70nj8U1BxCoJ.
>> >
>> > To post to this group, send email to django...@googlegroups.com.
>> > To unsubscribe from this group, send email to

>> > django-users...@googlegroups.com.


>> > For more options, visit this group at
>> > http://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/django-users/-/VqmbjpUWJpMJ.


>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to

> django-users...@googlegroups.com.

Tom Evans

unread,
Apr 3, 2012, 10:09:11 AM4/3/12
to django...@googlegroups.com
On Tue, Apr 3, 2012 at 1:14 AM, Homer <hi...@foxmail.com> wrote:
> I met "404" page not found when I try to enter 127.0.0.1:8000/cn/bedroom .
> It says on the webpage that "C:/Django/final/media/bedroom" does not exist".
> Why would this happen?
>
> urls.py:
>
> from django.conf.urls.defaults import *
> from final import settings
> from django.contrib import admin
> admin.autodiscover()
>
> urlpatterns = patterns('',
>
>     url(r'^admin/', include(admin.site.urls)),
>     url(r'^cn/', include('final.photo.urls')),
>     url(r'^cn/(?P<path>.*)$', 'django.views.static.serve',
>         {'document_root': settings.MEDIA_ROOT}),
> )
> photo/urls.py:
>
> from django.conf.urls.defaults import *
> from final.photo.views import List, Detail
> urlpatterns = patterns('',
>     url(r'^$', List),
>     url(r'^/bedroom/', Detail),
> )
>

You have conflicting URL space, you have configured "^cn/" to be both
where your static files are served from, and also where your views are
served from. Split the two up, and use the correct URL, and you should
not have any problems. Eg:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),

    url(r'^photos/', include('final.photo.urls')),
    url(r'^docs/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.MEDIA_ROOT}),
)

I'm not sure why people are interested in your
TEMPLATE_CONTEXT_PROCESSORS settings when the request isn't even
making it to the view...

Cheers

Tom

Sebastian Goll

unread,
Apr 3, 2012, 10:17:03 AM4/3/12
to django...@googlegroups.com
On Mon, 2 Apr 2012 17:14:00 -0700 (PDT)
Homer <hi...@foxmail.com> wrote:

> I met "404" page not found when I try to enter 127.0.0.1:8000/cn/bedroom .
> It says on the webpage that "C:/Django/final/media/bedroom" does not
> exist". Why would this happen?

I am positive the problem is with your URL patterns:

> # urls.py
> urlpatterns = patterns('',
> …


> url(r'^cn/', include('final.photo.urls')),
> url(r'^cn/(?P<path>.*)$', 'django.views.static.serve',
> {'document_root': settings.MEDIA_ROOT}),
> )
>

> # photo/urls.py


> urlpatterns = patterns('',
> url(r'^$', List),
> url(r'^/bedroom/', Detail),
> )

Trying to access 'cn/bedroom' _should_ redirect to view Detail, I guess.

But what happens is that 'cn/' gets stripped via the main level pattern,
including 'final.photo.urls'. There no match is found due to the leading
'/' in the second pattern: r'^/bedroom/'. Thus the search continues with
'cn/(?P<path>.*)$' in the top-level URL patterns which tries to access a
non-existing media file in settings.MEDIA_ROOT.

Therefore, to fix the issue, you should remove the leading forward slash
'/' in the second pattern in your photo/urls.py.

For reference, see the notes section in

https://docs.djangoproject.com/en/1.4/topics/http/urls/#example
– "There's no need to add a leading slash, because every URL has that.
For example, it's ^articles, not ^/articles."

Best wishes,
Sebastian.

Homer

unread,
Apr 3, 2012, 1:23:50 PM4/3/12
to django...@googlegroups.com
OK.
# Django settings for final project.



DEBUG = True

TEMPLATE_DEBUG = DEBUG



ADMINS = (

    # ('Your Name', 'your_...@example.com'),

)



MANAGERS = ADMINS



DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.

        'NAME': 'C:/Django/final/data.db',                      # Or path to database file if using sqlite3.

        'USER': '',                      # Not used with sqlite3.

        'PASSWORD': '',                  # Not used with sqlite3.

        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.

        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.

    }

}



# Local time zone for this installation. Choices can be found here:


# although not all choices may be available on all operating systems.

# On Unix systems, a value of None will cause Django to use the same

# timezone as the operating system.

# If running in a Windows environment this must be set to the same as your

# system time zone.

TIME_ZONE = 'America/Chicago'



# Language code for this installation. All choices can be found here:


LANGUAGE_CODE = 'en-us'



SITE_ID = 1



# If you set this to False, Django will make some optimizations so as not

# to load the internationalization machinery.

USE_I18N = True



# If you set this to False, Django will not format dates, numbers and

# calendars according to the current locale

USE_L10N = True



# Absolute filesystem path to the directory that will hold user-uploaded files.

# Example: "/home/media/media.lawrence.com/media/"

MEDIA_ROOT = 'C:/Django/final/media/'



# URL that handles the media served from MEDIA_ROOT. Make sure to use a

# trailing slash.


MEDIA_URL = '/media/'



# Absolute path to the directory static files should be collected to.

# Don't put anything in this directory yourself; store your static files

# in apps' "static/" subdirectories and in STATICFILES_DIRS.

# Example: "/home/media/media.lawrence.com/static/"

STATIC_ROOT = ''



# URL prefix for static files.


STATIC_URL = '/static/'



# URL prefix for admin static files -- CSS, JavaScript and images.

# Make sure to use a trailing slash.

# Examples: "http://foo.com/static/admin/", "/static/admin/".

ADMIN_MEDIA_PREFIX = '/static/admin/'



# Additional locations of static files

STATICFILES_DIRS = (

    # Put strings here, like "/home/html/static" or "C:/www/django/static".

    # Always use forward slashes, even on Windows.

    # Don't forget to use absolute paths, not relative paths.

)



# List of finder classes that know how to find static files in

# various locations.

STATICFILES_FINDERS = (

    'django.contrib.staticfiles.finders.FileSystemFinder',

    'django.contrib.staticfiles.finders.AppDirectoriesFinder',

#    'django.contrib.staticfiles.finders.DefaultStorageFinder',

)



# Make this unique, and don't share it with anybody.

SECRET_KEY = 'zg*^7-e5e(#wu6-543z0y#em+xvjo092h^%@*&qed^hqqa!2i5'



# List of callables that know how to import templates from various sources.

TEMPLATE_LOADERS = (

    'django.template.loaders.filesystem.Loader',

    'django.template.loaders.app_directories.Loader',

#     'django.template.loaders.eggs.Loader',

)



MIDDLEWARE_CLASSES = (

    'django.middleware.common.CommonMiddleware',

    'django.contrib.sessions.middleware.SessionMiddleware',

    'django.middleware.csrf.CsrfViewMiddleware',

    'django.contrib.auth.middleware.AuthenticationMiddleware',

    'django.contrib.messages.middleware.MessageMiddleware',

)



ROOT_URLCONF = 'final.urls'



TEMPLATE_DIRS = ("C:/Django/final/photo/templates"

    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".

    # Always use forward slashes, even on Windows.

    # Don't forget to use absolute paths, not relative paths.

)



INSTALLED_APPS = (

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.sites',

    'django.contrib.messages',

    'django.contrib.staticfiles',

    # Uncomment the next line to enable the admin:

    'django.contrib.admin',

    'final.photo',

    # Uncomment the next line to enable admin documentation:

    # 'django.contrib.admindocs',

)



# A sample logging configuration. The only tangible logging

# performed by this configuration is to send an email to

# the site admins on every HTTP 500 error.


# more details on how to customize your logging configuration.

LOGGING = {

    'version': 1,

    'disable_existing_loggers': False,

    'handlers': {

        'mail_admins': {

            'level': 'ERROR',

            'class': 'django.utils.log.AdminEmailHandler'

        }

    },

    'loggers': {

        'django.request': {

            'handlers': ['mail_admins'],

            'level': 'ERROR',

            'propagate': True,

        },

    }

}

TEMPLATE_CONTEXT_PROCESSORS = (

    'django.core.context_processors.request',

    'django.core.context_processors.debug',

    'django.core.context_processors.i18n',

    'django.core.context_processors.media',

    'django.core.context_processors.static',

    'django.contrib.auth.context_processors.auth',

    'django.contrib.messages.context_processors.messages',

)

在 2012年4月3日星期二UTC-4上午9时58分54秒,skhohlov写道:


>> >> >> For more options, visit this group at
>> >> >> http://groups.google.com/group/django-users?hl=en.
>> >> >>
>> >> >>
>> >> > --
>> >> > You received this message because you are subscribed to the Google
>> >> > Groups
>> >> > "Django users" group.
>> >> > To view this discussion on the web visit
>> >> > https://groups.google.com/d/msg/django-users/-/Os90mGtKgrIJ.
>> >> >
>> >> > To post to this group, send email to django...@googlegroups.com.
>> >> > To unsubscribe from this group, send email to


>> >> > For more options, visit this group at
>> >> > http://groups.google.com/group/django-users?hl=en.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Django users" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/django-users/-/70nj8U1BxCoJ.
>> >
>> > To post to this group, send email to django...@googlegroups.com.
>> > To unsubscribe from this group, send email to


>> > For more options, visit this group at
>> > http://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/VqmbjpUWJpMJ.
>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to

Homer

unread,
Apr 3, 2012, 1:23:53 PM4/3/12
to django...@googlegroups.com
OK.
TEMPLATE_CONTEXT_PROCESSORS = (

    'django.core.context_processors.request',

    'django.core.context_processors.debug',

    'django.core.context_processors.i18n',

    'django.core.context_processors.media',

    'django.core.context_processors.static',

    'django.contrib.auth.context_processors.auth',

    'django.contrib.messages.context_processors.messages',

)

在 2012年4月3日星期二UTC-4上午9时58分54秒,skhohlov写道:


>> >> >> For more options, visit this group at
>> >> >> http://groups.google.com/group/django-users?hl=en.
>> >> >>
>> >> >>
>> >> > --
>> >> > You received this message because you are subscribed to the Google
>> >> > Groups
>> >> > "Django users" group.
>> >> > To view this discussion on the web visit
>> >> > https://groups.google.com/d/msg/django-users/-/Os90mGtKgrIJ.
>> >> >
>> >> > To post to this group, send email to django...@googlegroups.com.
>> >> > To unsubscribe from this group, send email to


>> >> > For more options, visit this group at
>> >> > http://groups.google.com/group/django-users?hl=en.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Django users" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/django-users/-/70nj8U1BxCoJ.
>> >
>> > To post to this group, send email to django...@googlegroups.com.
>> > To unsubscribe from this group, send email to


>> > For more options, visit this group at
>> > http://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/VqmbjpUWJpMJ.
>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to

Homer

unread,
Apr 3, 2012, 1:30:55 PM4/3/12
to django...@googlegroups.com
If I split them up, I cannot see my pictures on the webpage since the links of them becomes "http://127.0.0.1:8000/cn/simages/XXX.jpg". But my pictures stored in "http://127.0.0.1:8000/media/simages/XXX.jpg"... That's why I mix them up...

在 2012年4月3日星期二UTC-4上午10时09分11秒,Tom Evans写道:

Homer

unread,
Apr 3, 2012, 1:41:36 PM4/3/12
to django...@googlegroups.com
I fixed it by changing the urlconf to r'^bedroom/'. Thanks!!!!!!!!!! 

在 2012年4月3日星期二UTC-4上午10时17分03秒,Sebastian写道:
Reply all
Reply to author
Forward
0 new messages