Django admin panel throws 404 when passing any url parameters
Example url:
/admin/app/model/ - OK
/admin/app/model/?foo=bar - 404
/admin/app/model/?p=1 - 404
Nginx+uwsgi
Project urls file (admin,admin_tools,application urls)
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
import restaurant.views
import club.views
import hotel.views
import custom_app.views
import cart.views
from django.views.decorators.csrf import csrf_exempt
from django.contrib import admin
import settings
handler404 = 'custom_app.views.handler404'
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin_tools/', include('admin_tools.urls')),
(r'^$', custom_app.views.Index.as_view()),
(r'^restaurant$', restaurant.views.Index.as_view()),
(r'^which$', csrf_exempt(TemplateView.as_view(template_name='custom_app/which.html'))),
(r'^cart/add', cart.views.AddToCart.as_view()),
(r'^cart/delete', cart.views.RemoveFromCart.as_view()),
(r'^cart/send', cart.views.SendCart.as_view()),
(r'^cart/delivery', cart.views.SetDelivery.as_view()),
(r'^restaurant/about', restaurant.views.About.as_view()),
(r'^restaurant/payment', restaurant.views.Payment.as_view()),
(r'^restaurant/vip', restaurant.views.Vip.as_view()),
(r'^restaurant/menu/(?P<slug>.+)', restaurant.views.Menu.as_view()),
(r'^restaurant/menu/', restaurant.views.Menu.as_view()),
(r'^restaurant/cart/', cart.views.Basket.as_view()),
(r'^restaurant/tables/', restaurant.views.Tables.as_view()),
(r'^restaurant/success_ordering', restaurant.views.SuccesTableOrder.as_view()),
(r'^vacancy', custom_app.views.Vacancy.as_view()),
(r'^hotel$', hotel.views.Main.as_view()),
(r'^hotel/services', hotel.views.Services.as_view()),
(r'^hotel/room/(?P<room>\d+)$', hotel.views.Main.as_view()),
(r'^hotel/order/option/toggle$', hotel.views.ToggleOption.as_view()),
(r'^hotel/order/date/toggle$', hotel.views.ToggleDate.as_view()),
(r'^hotel/order/send$', hotel.views.Send.as_view()),
(r'^club/events/old/(?P<year>\d+)/(?P<month>\d+)', club.views.OldEvents.as_view()),
(r'^club/events/old/', club.views.OldEvents.as_view()),
(r'^club/about', club.views.About.as_view()),
(r'^club/event/(?P<pk>\d+)', club.views.DetailEvent.as_view()),
(r'^club$', club.views.Main.as_view()),
url(r'^admin/', include(admin.site.urls)),
(r'^ckeditor/', include('ckeditor.urls')),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
Nginx configuration file:
server
{
listen %server_ip%;
server_name custom.ru www.custom.ru;
root /home/custom/www/;
location ~* ^/resize/([\d\-]+)/([\d\-]+)/(.+)$ {
alias /home/custom/www/custom/$3;
image_filter resize $1 $2;
image_filter_buffer 2M;
error_page 415 = /empty;
}
location ~* ^/crop/([\d\-]+)/([\d\-]+)/(.+)$ {
alias /home/custom/www/custom/$3;
image_filter crop $1 $2;
image_filter_buffer 2M;
error_page 415 = /empty;
}
location = /empty {
empty_gif;
}
location /
{
root /home/custom/www/custom/;
uwsgi_pass unix:///home/custom/tmp/uwsgi.sock;
include uwsgi_params;
uwsgi_param UWSGI_PASS unix:///home/custom/tmp/uwsgi.sock;
uwsgi_param UWSGI_CHDIR /home/custom/www/custom/;
uwsgi_param UWSGI_SCRIPT wsgi;
}
location /static
{
alias /home/custom/www/custom/static/;
}
location /media
{
alias /home/custom/www/custom/media/;
}
location = /favicon.ico { alias /home/custom/www/favicon.ico; }
}
Try
/admin/app/action?foo=bar
instead of /?
The extra / is looking for a resource at that location
Related
I am trying to build a simple blog application on an ubuntu droplet hosted on digital ocean.
I have the DJango app in mid development and I am getting a No reverse match for a view that existes
Here is the error:
'NoReverseMatch at / Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name.'
Here is my Url Patterns:
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
]
~
Here is My view
from django.shortcuts import render, get_object_or_404
def post_list(request):
posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, 'blog/post_list.html', {'posts': posts})
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
~
Here is My nginx file:
server {
listen 80;
server_name XXX.XXX.XXX.XXX;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/XXXX/Django;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/XXX/Django/BizBlog.sock;
}
}
Where is the error occurring? I can't figure this out, I have worked with Django before and know it has to do with the url searching for the string patterns in my view function and being unable to find it but the function is there. What is wrong?
Here is my main url.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
url(r'', include('blog.urls')),
]
Just stop the django development server process, restart the services waitress on windows/gunicorn on linux and nginx on windows/linux.
Currently on django-cms 2.4.2. Encountered a peculiar problem, when on local machine where DEBUG=True, visiting 127.0.0.1/ will redirect to 127.0.0.1/en/. When running on a production server (virtualenvs, apache), visiting mysite.com/ will raise a 404 but display the home page behind while mysite.com/en/ displays the home page. Been trying to find the root of the problem but can't seem to figure out if it's in the urls, wsgi or settings.
urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin_tools/', include('admin_tools.urls')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += i18n_patterns('',
url(r'^accounts/', include('allauth.urls')),
... apps ...
url(r'^', include('cms.urls')),
)
# Static files
urlpatterns += staticfiles_urlpatterns()
if settings.DEBUG:
urlpatterns = patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
else:
from django.conf.urls.defaults import *
handler500 = 'error_pages.views.server_error'
handler404 = 'error_pages.views.custom_404'
handler403 = 'error_pages.views.permission_denied'
handler400 = 'error_pages.views.bad_request'
I have django.middleware.locale.LocaleMiddleware and cms.middleware.language.LanguageCookieMiddleware added in settings.
In my Apache httpd.conf:
NameVirtualHost *:27567
# Begin site configuration
<VirtualHost *:27567>
WSGIScriptAlias / /path/to/wsgi.$
ErrorLog "/path/to/log/error_path.log"
</VirtualHost>
Tried logging any errors that django might encounter and only received:
WARNING 2013-09-18 14:04:34,380 Not Found: /
Edit:
Seems like when Debug=True, the problem goes away. I have set allowed_hosts settings to 'localhost' and 'domain.com'. Have checked sequence of middleware loaded, i18n is set to true etc but still can't pinpoint where the issue lies.
Edit 2:
Just found the problem. It lies in the error pages where the views for error pages are:
from django.shortcuts import render
def server_error(request):
return render(request, '500.html')
Most likely a clash between django-cms' urls.
I am a new user to Django and Django Cms.
on local machine
all js, html,css files download well when i work with backend
But on production when i use text plugin I can't get some files
There are:
GET http://example.com/admin/cms/page/15/edit-plugin/56/lang/uk.js/ 500 (Internal Server Error)
GET http://example.com/admin/cms/page/15/edit-plugin/56/iframe/default/wymiframe.html/ 500 (Internal Server Error) (Status Code:301 MOVED PERMANENTLY)
Where example.com - my own site:)
File uk.js - i added by monkey patching. (everything work fine on local machine)
In my html code in local I have:
<iframe src="/static/cms/wymeditor/iframe/default/wymiframe.html" onload="this.contentWindow.parent.WYMeditor.INSTANCES[0].initIframe(this)"></iframe>
On production side:
<iframe src="iframe/default/wymiframe.html" onload="this.contentWindow.parent.WYMeditor.INSTANCES[0].initIframe(this)"></iframe>
My urls.py:
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('core.urls')),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
my core urls.py:
from django.conf.urls import patterns, url, include
from django.http import HttpResponse
urlpatterns = patterns('core.views',
url(r"^robots.txt$", lambda r: HttpResponse("User-agent: *\nDisallow: /", mimetype="text/plain")),
url(r"^commissions/$","commissions", name = "commissions" ),
url(r"^commissions/(?P<slug>[a-zA-Z0-9_\-]+)/$","commissions", name = "comma" ),
url(r'^news/', include('cmsplugin_news.urls'),{}),
url(r'^', include('cms.urls')),
)
WHen i do python manage.py collectstatic - everything fine and i have all static files on my static folder
Please help me what i doing wrong ?
I am using Django 1.4 for my project Builder
If I get a request like http://xxx.xx.xx:8000/index.html, how can I redirect to /home/?
Redirect all /index.html requests to /home/ using generic.simple.redirect_to
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
(r'^index.html$', redirect_to, {'url': '/home/'}),
(r'^/home/$', 'myapp.views.my_home_view'),
)
You need to add an url to your urlconf
urlpatterns = patterns('',
...
(r'^index\.html$', 'myapp.views.my_home_view'),
...
)
EDIT
An alternative option is to make use of the django.contrib.redirects app, and add an entry via the admin that redirects from index.html to home/. This is quicker and more configurable
Use redirect_to method of generic.simple:
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
(r'^home/$', 'yourapp.views.home', name='home'),
(r'^index.html$', redirect_to, {'url': '/home/'}),
)
I am trying to deploy my django project which is located at home/doga/headend/ and just to run it on the localhost (will be a LAN accessable project). My main problem is that I can use the site well however the /admin/ folder is giving me Internal Server Error error.
anyway here is my etc/apache2/sites-available/default file
<VirtualHost *:80>
ServerName /
ServerAlias */
DocumentRoot /home/doga/headend/
LogLevel warn
WSGIScriptAlias / /home/doga/headend/apache/django.wsgi
Alias /media /home/doga/headend/media/statics
Alias /admin_media /usr/lib/python2.4/site-packages/django/contrib/admin/media
</VirtualHost>
and here is my home/doga/headend/apache/django.wsgi file
import os, sys
import django.core.handlers.wsgi
sys.path.append('/home/doga/')
sys.path.append('/home/doga/headend')
os.environ['DJANGO_SETTINGS_MODULE'] = 'headend.settings'
application = django.core.handlers.wsgi.WSGIHandler()
lastly my main url.py
from django.conf.urls.defaults import *
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^headend/', include('headend.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
(r'^table/(?P<pid>.*)/$', 'main.views.table_view'),
(r'^graph/(?P<pid>.*)/$', 'main.views.graph_view'),
(r'^graph/$', 'main.views.platform_graph_view'),
(r'^table/$', 'main.views.platform_view'),
(r'^csv/$', 'main.views.csv_view'),
(r'^recent/$', 'main.views.recent_view'),
(r'^$', 'main.views.main_view'),
(r'^cs/(?P<number>.*)/$', 'main.views.ch_view'),
#(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
#(r'^$', 'main.views.main_view'),
#(r'^media/(?P<path>.*)$', 'django.views.static.serve',
# {'document_root': '/home/uluc/headendmedia/statics'}),
)
I don't believe you should be setting the DocumentRoot to /home/doga/headend. Won't this give access to all your source code?
What detail does the Apache log give for the Internal Server Error?