Redirect in Django admin page - django

In my django project, after activating the admin application, I canno't get access to the page localhost/soundaxis/admin because I'm redirected to localhost/admin.
I think the problem has to do with the fact that the project was not located at the base address.
Urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^soundaxis/', 'views.index.home'),
(r'^soundaxis/admin/', include(admin.site.urls)),
)

Start the server by typing the following in the base dir:
python manage.py runserver
then go to http://localhost:8000/
Its rather complicated to get the project to run off another subdirectory so I suggest running it from the base, especially during development. Later in the game you can try work it from a subdir if required, but better yet would be subdomain.

Related

How can I host a Django web app on Cpanel out the public_html folder?

I am trying to host a Django web application on Cpanel.
However, my hosting service is having a main folder which is called public_html. In this folder, there is the index page. My project folder named myapp is out of the public_html folder. Whenever I run the application, it is showing the content of the index.html which is in the public_html folder instead of running the home page of my application that should be executed from this main urls.py file.
Bellow is the main urls.py content.
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('depenses/', include('depenses.urls', namespace='depenses')),
path('cart/', include('cart.urls', namespace='cart')),
path('orders/', include('orders.urls', namespace='orders')),
path('coupons/', include('coupons.urls', namespace='coupons')),
path('', include('shop.urls', namespace='shop')),
# path('', TemplateView.as_view(template_name='templates/index.html'), name='home'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Again my project is out of the public_html folder.
Please assist me to host my application.
This is normal behavior considering the fact that the web service will attempt to serve HTML/PHP files only.
You will need to deploy your Django app through a python interpreter like Apache Passenger or something similar that would handle and serve it.
This can be easily achieved without many configurations and hassles with the Python App feature of CloudLinux.
Therefore, I would suggest you simply find a hosting provider that provides CloudLinux's Python App feature. The deployment process via that feature is with just a few simple clicks.

django ImportError at /admin/ no module named todo

I am following a tutorial on http://www.lightbird.net/dbe/todo_list.html to create a simple todo app. In one of the steps, I had to modify view to add an ability in 'admin' to mark tasks as done from that view. However I get the error ImportError at /admin/ no module named todo.
The error is not thrown from any particular line from the code so I do not know how to debug this. I am new to django. So I documented my error in my blog here: http://djangounchain.wordpress.com/2013/01/10/tutorial-8-todo-list-app/
Hope someone can help me!
You are registering your models to AdminSite in todo/models.py itself.
As per official django documentation, you need to create admin.py file inside your app for admin.autodiscover() to work properly.
The last step in setting up the Django admin is to hook your AdminSite
instance into your URLconf. Do this by pointing a given URL at the
AdminSite.urls method.
In this example, we register the default AdminSite instance
django.contrib.admin.site at the URL /admin/
# urls.py
from django.conf.urls import patterns, url, include
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
Above we used admin.autodiscover() to automatically load the
INSTALLED_APPS admin.py modules.

Django urls.py not updating to include heroku url?

I am trying to deploy my Django app on Heroku and I am following the tutorial but when I go and try to open the website with:
heroku open
I receive the error :
Request URL: http://nameless-dawn-7713.herokuapp.com/
Using the URLconf defined in crunchWeb.urls, Django tried these URL patterns, in this order:
^crunchApp/results$
^crunchApp/contact$
^admin/
The current URL, , didn't match any of these.
Even though before running " heroku open" my URLs.py file looks like this (saved) :
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('crunchApp.views',
url(r'^crunchApp/results$', 'results'),
url(r'^http://nameless-dawn-7713.herokuapp.com/$', 'contact'),
url(r'^crunchApp/contact$', 'contact'),
url(r'^admin/', include(admin.site.urls)),
)
I think I might have to change the urls.py before the command:
git commit -m "my django app"
but I dont know the URL that it will need until the command:
heroku create
Any ideas of what to do?
Why do you have url(r'^http://nameless-dawn-7713.herokuapp.com/$', 'contact') in your urls file ? In fact, these URLs should be relative to your application, and you should not update this file before uploading to Heroku (if it works locally, it should work on Heroku without any change to this file).
If you want to connect the contact view to your base URL (http://nameless-dawn-7713.herokuapp.com/), you should use something like:
url(r'^$', 'contact')
Hope this helps !

My DJango app is responding to /static urls

Update
I figured out what was causing the stylesheets to become invisible, though I don't quite understand it all. I set DEBUG=False in settings.py to test the error handling as described in the tutorial. Somehow setting debug to false causes the static files not to be locatable. I will look further in the configs to see if I can get a clear understanding why. Until then, please feel free to answer or comment with additional info. I am still learning!
Update
I'm going through a DJango tutorial from here and I hit a roadblock. I'm up to tutorial 3 where they explain how to refactor your urls.py file when I try loading up the admin site to make sure I haven't broken it. Sure enough it looked all wierd because it was missing the stylesheets. Stylesheets are pulled from here:
http://127.0.0.1:8000/static/admin/css/base.css
When I hit that link in my browser I get the custom 404 page I configured for my app. The stylesheets were working prior but I'm not sure which change broken them. I went through my urls.py file and reverted all of the polls specific url configs to no avail. Here's my current urls.py under hellodjango (the name of my project.)
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.http import HttpResponse
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
def page_not_found(request, template_name='404.html'):
return HttpResponse("Could not find the resource you asked for...")
handler404 = 'hellodjango.urls.page_not_found'
and here's the urls.py under my polls directory:
from django.conf.urls import patterns, url
# Uncomment the next two lines to enable the admin:
urlpatterns = patterns('polls.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
Help?
It looks like you don't have a URL pattern for /static. As such, the static/admin/css/base.css URL doesn't match any pattern, and so you get a 404. Try something like this:
from django.conf.urls.static import static
# ...
urlpatterns = patterns('',
# ...
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
# ...
This should work for you -- go to /static/foo.css, and you should see your CSS.
It's worth noting that this is discouraged in a production environment. For your tutorial app, though, it'll work.
The staticfiles app provides a custom runserver management command that automatically serves the static files, are you sure you have the following in your settings?
INSTALLED_APPS = (
# ...
'django.contrib.staticfiles',
)
In production, you'll use the collectstatic management command that finds all of the static media and dumps it into STATIC_ROOT (this is the only purpose for this setting - it isn't used or needed during development).
Glad you figured it out. Here's why it works like this.
django.contrib.staticfiles overrides the runserver management command so that the static files are served automatically. To remind people that they shouldn't be using django to serve static files, this only happens when DEBUG = True, as you found out.
The documentation of the overridden management command explains that you can use the --insecure flag to make this work no matter the state of the DEBUG setting.

Registered models do not show up in admin

I added a model to admin via admin.site.register, and it does not show up in admin. Since admin is so "It just works", I have no idea of how to debug this. Pointers?
After adding and registering your admin:
# app/admin.py
class YourModelAdmin(admin.ModelAdmin):
pass
admin.site.register(YourModel, YourModelAdmin)
Make sure your app is in your project settings.py:
# settings.py
INSTALLED_APPS = (
# other apps ...
'app',
)
Sync your project for that model if you have not done so already:
python manage.py syncdb
Restart your server, CTRL-C:
python manage.py runserver
In such a situation is also a good practice to check if the user logged in to the admin panel has rights to manage such a model. If they do then you could change your code to access the functions as root.
When in doubt, shut down server, syncdb, start server.
I have the experience, that sometimes after changing an admin.py the dev-sever won't be restarted. in that case touch settings.py helps.
I think the checklist in Thierry's answer is almost definitive, but make sure that urls.py contains admin.autodiscover() to load INSTALLED_APPS admin.py modules.
# urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
('^admin/', include(admin.site.urls)),
)
More info in the django docs.
Have you added the application to your installed apps? That has happened to me both one and two times. :) Otherwise it would be useful for us to see the code to help you.
Also make sure there are no syntax errors in your admin.py or anything. That can cause an app to fail to be registered with the AdminSite.
I've faced the same problem, but it was a little tricky than yours.
Consider, that you have a project with, say, five or even more apps. For me it is more obvious to register all models in just one admin.py file, so I have decided to do it in one place - core directory. Of course, it was not an app, so none of models showed up on admin page.
comment out the some lines in urls.py see docs for more details
admin.autodiscover()
urlpatterns = patterns('',
('^admin/', include(admin.site.urls)),
)