Django cannot find admin.sites.url - django

I am developing a django site locally using rc1.3 and after making several adjustments to my urls.py file i am receiving error messages that django cannot import admin.site.urls. by urls.py file is below.
urlpatterns = patterns('',
# Example:
(r'^city/$', 'venue.views.city_index'),
(r'^accounts/', include('registration.urls')),
(r'^accountss/', include('registration.backends.simple.urls')),
(r'^profiles/', include('profiles.urls')),
(r'^admin/', include('admin.site.urls')),
(r'^city/(?P<city>[-\w]+)/$', 'venue.views.city_detail'),
(r'^city/(?P<city>[-\w]+)/venue/$', 'venue.views.venue_index'),
)
Any idea what might be cause this, thanks.

http://docs.djangoproject.com/en/dev/intro/tutorial02/#activate-the-admin-site
Don't include the string path, actually import the admin module and point to its urls.
from django.contrib import admin
urlpatterns += patterns('',
(r'^admin/', include(admin.site.urls)),
)

Related

Display static page in Django

I am trying to display contents of a static page in Django project.
urls.py :-
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'spollow.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
(r'^$', 'django.views.generic.simple.direct_to_template', {'template': 'index.html'}),
url(r'^admin/', include(admin.site.urls)),
)
index.html is in the same directory as urls.py
I am getting 500 internal server error. Any ideas where I am going wrong?
First of all, what is the stacktrace from the 500 error saying that the error may be? You may be using Django 1.6 and the call to direct_to_template is deprecated.
On Django 1.5 or newer you can use TemplateView
Here's the example from the documentation
https://docs.djangoproject.com/en/dev/topics/class-based-views/
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^about/', TemplateView.as_view(template_name="about.html")),
)
You can use the direct_to_template view on Django 1.4 or older
Here's the relevant documentation
https://docs.djangoproject.com/en/1.4/ref/generic-views/#django-views-generic-simple-direct-to-template
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^foo/$', direct_to_template, {'template': 'foo_index.html'}),
(r'^foo/(?P<id>\d+)/$', direct_to_template, {'template': 'foo_detail.html'}),
)
If it is the latter, I would use a module instead of string, (look at the import on the example).
Other than that, without the 500 details it will be shooting in the dark, you may not have the right template, or an incorrect path, or a million different things.
Bonus note
If you just want to serve static pages, it might be better to serve them through the actual webserver in front of django (nginx, apache, etc), specially if you are expecting a high volume of traffic.
If Your error is due to unable to find index.html
if yours is an app(ie: created by python manage.py startapp <app>) then:
Then django will search for template files in <app>/templates directory, if you added the app to INSTALLED_APPS in settings.py.
so you need to create a folder templates inside your <app> and put index.html inside it.
if you don't have any apps, you want to add the template path manually :
open settings.py, then edit TEMPLATE_DIRS
TEMPLATE_DIRS = (
# Put the full path of the template dir here, like "/home/html/django_templates" or
# "C:/www/django/templates".
)
In Django 1.5 or newer you can use the render function instead of direct_to_template:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'django.shortcuts.render', {'template_name': 'index.html'}),
)
Or if you prefer the more complex way :), you can use class-based TemplateView:
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name="index.html")),
)

How to add more than one link in Django 1.6.1 urls.py

I am trying to add more than one link to my urls.py. First code below is in the app url, and other one in the main urls. However; when I am trying to add REGISTER and run the server, it still displays me the same of BLOG page.
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^blog/', include('blog.urls')),
url(r'^register/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),)
This one in the main urls.py
from django.conf.urls import patterns, url
from blog import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^$', views.register, name='register'),)
What should I do to fix this situation? Any idea will be appreciated. Thanks in advance
Your main urls.py should have:
url(r'^register/$', views.register, name='register'),)
and you should remove the register from app urls.
This will make yourapp.com/register/ point to views.register (or possibly yourapp.com/blog/register/ view it - I'm slightly confused on which urls.py is taking precedence)
The first block of code should be in your project's folder/project folder/urls.py, so paste it there. The second block is perfect, it should be placed in the new urls.py that you have created recently in the apps folder.

Redirect after login not working

I've been searching around but can't figure out why the redirection after login is not working as expected.
I successfully login at http://localhost:8000/accounts/login/. After that I'm always redirected to http://localhost:8000 no matter if I use settings.py variable LOGIN_REDIRECT_URL or if I leave it completely out.
# urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^grappelli/', include('grappelli.urls')),
url(r'^$', 'myapp.views.home', name='home'),
url(r'^home/$', 'myapp.views.apphome', name='apphome'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('registration.backends.default.urls')),
(r'^admin/', include("massadmin.urls")),
)
I don't know where I should start looking for a problem. Any ideas? Is there some settings I'm missing here? Any other files I should include to help debug the problem?
Check your form action! (glad I could help :) )

Django: 404 error in urls.py

I have two urls.py files.
In project/urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
In project/blog/urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('blog.views',
(r'^$', 'index'),
(r'^(?P<slug>[a-z-]+)/$', 'detail'),
(r'^(?P<slug>[a-z-]+)/comment/$', 'comment'),
)
Then I tried to browse these URLs:
http://127.0.0.1:8888/ (404)
http://127.0.0.1:8888/hello-world/ (404)
http://127.0.0.1:8888/admin/ (It worked)
Django version: 1.4 pre-alpha SVN-16985.
Thanks!
To fix the http://127.0.0.1:8888/ error, change your regular expression to:
(r'^$', include('blog.urls')),
Regarding the http://127.0.0.1:8888/hello-world/ error: There could be many things causing this. First, check that you have a blog title that actually returns the slug 'hello-world'.

Django bug : ImproperlyConfigured at /databrowse/ . How to solve this bug?

My urls.py of the project looks like this
from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib import admin
from django.contrib import databrowse
from world.views import welcome
admin.autodiscover()
urlpatterns = patterns('',
(r'^$', welcome),
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^databrowse/', include(databrowse.site.root)),
)
urlpatterns += patterns('',
(r'^media/$',include('django.views.static.serve'),{'document_root': settings.MEDIA_ROOT, 'show_indexes': True})
)
What changes have to be done for this to work ?
The only change you have to make is to follow the actual documentation. The documentation doesn't use include for databrowse.