django - Page not found (404) - python

this is my first django app and I Have received the following msg:
Page not found (404)
Request Method: GET
Request URL:
Using the URLconf defined in _3Ms.urls, Django tried these URL patterns, in this order:
^ ^$ [name='vista_principal']
^admin/doc/
^admin/
The current URL, home/, didn't match any of these.
my setting.py
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
)
# 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 = '7th)hsx0w5mwlv+oty62w(us7&xtiw#y0&12)67ld%6y1-a)4f'
# 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',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = '_3Ms.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = '_3Ms.wsgi.application'
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates'),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'apps',
)
_3Ms/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', '_3Ms.views.home', name='home'),
# url(r'^_3Ms/', include('_3Ms.foo.urls')),
url(r'^', include('apps.es.urls')), # incluye las urls de es.urls.py
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
es/urls.py
from django.conf.urls import patterns, url
urlpatterns = patterns('apps.es.view',
url(r'^$', 'home_view', name='vista_principal'),
)
es/views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
def home_view(request):
return render_to_response('es/home.html', context_instance=RequestContext(request))

You don't have a url for home/.
If you added url(r'^home/$', 'home_view', name='vista_principal'), it would have something to go to.

Related

api.urls are not working after using django-hosts for subdomain

i'm trying to use sub-domain for api in my project and using django-hosts for that.but after implementing it as documented there is Page not found (404) error is coming when I'm accessing any URL of my app 'api'
project/hosts.py
from django_hosts import patterns, host
from django.conf import settings
host_patterns = patterns('',
host(r'www', settings.ROOT_URLCONF, name='www'),
host(r'api', 'apps.api.urls', name='api'),
)
project/settings.py
def ip_addresses():
ip_list = []
for interface in netifaces.interfaces():
addrs = netifaces.ifaddresses(interface)
for x in (netifaces.AF_INET, netifaces.AF_INET6):
if x in addrs:
ip_list.append(addrs[x][0]['addr'])
return ip_list
ALLOWED_HOSTS = ip_addresses()
ALLOWED_HOSTS.append('.localdemo.in')
ALLOWED_HOSTS.append('.api.localdemo.in')
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'apps.core',
'apps.api',
# Third party
'django_hosts',
]
MIDDLEWARE = [
'django_hosts.middleware.HostsRequestMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'drf_api_logger.middleware.api_logger_middleware.APILoggerMiddleware',
'django_hosts.middleware.HostsResponseMiddleware',
]
ROOT_URLCONF = 'project.urls'
ROOT_HOSTCONF = 'project.hosts'
DEFAULT_HOST = 'www'
project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('apps.core.urls')),
]
handler404 = "apps.core.views.customerrors.custom_handler404"
handler403 = "apps.core.views.customerrors.custom_handler403"
apps/api/urls.py
from django.urls import path, include, re_path
from django.conf.urls import url
from .views import statisticsview
urlpatterns = [
path('v1/statistics', statisticsview.StatisticsListView.as_view(), name='statistics_list'),
re_path(r'^statistics/$', statisticsview.StatisticsListView.as_view(), name='statistics_list'),
re_path(r'statistics/', statisticsview.StatisticsListView.as_view(), name='statistics_list'),
hosts file inside
windows/system32/drivers/etc/hosts
127.0.0.1 localdemo.in
127.0.0.1 api.localdemo.in
now when I'm accessing localdemo.in:8000 its working fine but I'm to unable to access api.localdemo.in:8000/v1/statistics/
there is page not found error and i'dont know why tried every urlpatter written in api/urls.py
only URL is working in which URL path is empty like this one at api.localdemo.in:8000 but others are not working(page not found error at api.localdemo.in:8000/v1/statistics/)
re_path('', statisticsview.StatisticsListView.as_view(), name='statistics_list'),

Django + Zinnia: Can not GET image for entry's illustration

I'm using Django (1.7.3) with zinnia blog (0.15.1) and I'm having an issue for rendering the 'image for illustration' that you can add for a blog entry.
I have the following urls.py files:
My top-dir url conf
# -*- coding: utf-8 -*-
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^ecv/', include('extended_cv.urls',namespace="ecv")),
url(r'^weblog/', include('zinnia.urls', namespace='zinnia')),
url(r'^comments/', include('django_comments.urls')),
)
The settins.py file:
# -*- coding: utf-8 -*-
** Snip**
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
# For using django.contrib.sites
SITE_ID = 2
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pro',
'extended_cv',
'zinnia', # Added for zinnia blog usage
'django.contrib.sites',# Added for zinnia blog usage
'django_comments',# Added for zinnia blog usage
'mptt',# Added for zinnia blog usage
'tagging',# Added for zinnia blog usage
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.i18n',
'django.core.context_processors.request',
'zinnia.context_processors.version',
)
** Snip**
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
When I create an entry for zinnia using admin UI, and when I add an image, no error appear, and the new entry is saved to database with no warning. The image then appear in the server tree file at the adress TopDir/upload/zinnia/date/of/publication/index_IKd2N0d.jpeg
But, when I read the new entry from the weblog app, I correctly see the new entry, but not the image. Moreover, the server output the following error:
GET /weblog/2015/01/18/1st-entry/uploads/zinnia/2015/01/18/index_IKd2N0d.jpeg HTTP/1.1" 404 8922
I guess the issue is kind of complicated to debug, then if you need more information, I'll be happy to give you more.
Thank you.
As explain by Daniel Roseman in the comments, the answer consists into defining the variables MEDIA_URL and MEDIA_ROOT, and to rewrite the top urls.py file as explained in https://docs.djangoproject.com/en/1.7/howto/static-files/#serving-files-uploaded-by-a-user-during-development:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^ecv/', include('extended_cv.urls',namespace="ecv")),
url(r'^weblog/', include('zinnia.urls', namespace='zinnia')),
url(r'^comments/', include('django_comments.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And the work is done.

500 Error Django file upload

I have been using this code to produce a file uploader and I can only get as far as getting the template to attempt an upload. I am wondering if I need to either configure Apache, add something to Django settings or install some more libraries maybe?
I have followed all the steps mentioned on that page, but please ask if you suspect I may have carried one or two of them out incorrectly.
Error
jssor.slider.js
Error INTERNAL SERVER ERROR
Settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_file_form',
'django_file_form.ajaxuploader',
'jfu',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
STATIC_ROOT = '/Users/Jonathan/Documents/django/bible/bible/static/jfu'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'static').replace('\\','/'),
)
views.py
from django.conf import settings
from django.core.urlresolvers import reverse
from django.views import generic
from django.views.decorators.http import require_POST
from jfu.http import upload_receive, UploadResponse, JFUResponse
#require_POST
def upload( request ):
# The assumption here is that jQuery File Upload
# has been configured to send files one at a time.
# If multiple files can be uploaded simulatenously,
# 'file' may be a list of files.
file = upload_receive( request )
instance = YOURMODEL( file = file )
instance.save()
basename = os.path.basename( instance.file.path )
file_dict = {
'name' : basename,
'size' : file.size,
'url': settings.MEDIA_URL + basename,
'thumbnailUrl': settings.MEDIA_URL + basename,
'deleteUrl': reverse('jfu_delete', kwargs = { 'pk': instance.pk }),
'deleteType': 'POST',
}
return UploadResponse( request, file_dict )
#require_POST
def upload_delete( request, pk ):
success = True
try:
instance = YOURMODEL.objects.get( pk = pk )
os.unlink( instance.file.path )
instance.delete()
except YOURMODEL.DoesNotExist:
success = False
return JFUResponse( request, success )
urls.py
from django.conf.urls import patterns, include, url
from bible import views
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'bible.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^verses/', views.index),
url(r'^search/', views.search),
url(r'^contact/', views.contact),
url(r'^profile/', views.profile),
url(r'^register', views.register),
url( r'upload/', views.upload, name = 'jfu_upload' ),
# You may optionally define a delete url as well
url( r'^delete/(?P<pk>\d+)$', views.upload_delete, name = 'jfu_delete' ),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
models.py
from django.db import models
from django.forms import ModelForm
from django.conf import settings
# Create your models here.
class Photo( models.Model ):
file = models.FileField( upload_to = settings.MEDIA_ROOT )
The issue was with the database. I think it was corrupted somehow and wouldn't talk with the application properly. Rebuilding it and running python manage.py migrate seemed to do the trick.

Django admin template is broken in production

I'm working with django, and trying to deploy my app to heroku.
All is working without any problems in local (even with DEBUG=False), but when deployed to heroku, the admin template doesn't display when DEBUG=False.
I followed theses instructions to configure my settings.py : https://devcenter.heroku.com/articles/django-assets
And here is my Procfile :
web: gunicorn bourse_logements.wsgi -b 0.0.0.0:$PORT
Feel free to ask if yu need some parts of my settings.py, I will paste them
Any help would be appreciated
EDIT :
Here is my settings.py :
https://gist.github.com/e-goz/62f812ab1fa8f8268f94
Are you sure you didn't .gitignore the templates folder?
* Add This line to your setting.py*
ADMIN_MEDIA_PREFIX = '/static/admin/'
you have to also copy all admin CSS and JavaScript to your static path(within static folder)
like static/admin/"you staticfiles"
You can try This setting as per your configuration on setting.py.
from unipath import Path
PROJECT_DIR = Path(__file__).ancestor(3)
PROJECT_ROOT = Path(__file__).ancestor(2)
sys.path.insert(0, Path(PROJECT_ROOT, 'apps'))
MEDIA_ROOT = PROJECT_DIR.child("media")
MEDIA_URL = '/media/'
STATIC_ROOT = PROJECT_DIR.child("collected_static")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
PROJECT_DIR.child("static"),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
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',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)
TEMPLATE_DIRS = (
Path(PROJECT_ROOT, 'templates'),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.flatpages',
)
# Heroku specific settings
import dj_database_url
DATABASES['default'] = dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# You can also try in your url.py
if settings.LOCAL_DEV:
baseurlregex = r'^media/(?P<path>.*)$'
urlpatterns += patterns('',
(baseurlregex, 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

Django 1.4.1 not loading admin site (Django Book tutorial)

I can't figure out why Django is not loading the admin page. It seems like it isn't even reading the urls.py file that I am editing - because even if I comment out the 'urlpatterns' statement, it still loads the local hello page fine once I run the server.
This is the error message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/admin
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^hello/$
^time/$
^time/plus/(\d{1,2})/$
The current URL, admin, didn't match any of these.
This is my urlpatterns code:
from django.conf.urls import patterns, include, url
from mysite.views import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
('^hello/$', hello, ),
('^time/$', current_datetime, ),
(r'^time/plus/(\d{1,2})/$', hours_ahead, ),
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls))
)
And this is a snippet os my settings.py file:
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'
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'mysite.wsgi.application'
TEMPLATE_DIRS = (
'/Users/pavelfage/Desktop/Coding/mysite/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',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'mysite.books'
)
Any help much appreciated!
I faced same problem. Uncommenting from django.contrib import admin in urls.py solved the problem.
I had the same problem. You may try this:
In your urls.py replace the call include(admin.site.urls) by this: admin.site.urls
In your setting.py if don't have any TEMPLATE_CONTEXT_PROCESSORS property (that was my situation) add this:
TEMPLATE_CONTEXT_PROCESSORS = ("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.static", "django.core.context_processors.tz", "django.contrib.messages.context_processors.messages")
It seams to be roughly the default property in a normal django 1.4 configuration. Here are the docs talking about it: djangoproject-doc1 djangoproject-doc2
You may also have to uncomment the strings:
# 'django.contrib.messages',
# 'django.contrib.staticfiles',
in your INSTALLED_APPS property of the settings.py but i'm not sure about it.
Sorry about not explaining much better the reasons of those changes but I'm a django-beginner to. I just found your question corresponding to my problem and then a possible awnser.
I hope it may help you.
EDIT: as seen in a comment you may try to remove the url(...) instruction on the line concerning the url of the admin site
ben

Categories

Resources