1

I am following a tutorial for django, im hosting my project in a free hosting for testing,

the hosting is working fine with python ,and the django shell for example,

but I cannot see the proper data in my index.html or have acces to the /admin

so I think is a problem with a wrong path?,

so please advice on this noob issue,

this is the folder where I have my files /home/mako34/www/blog

here my code:

I suppose settings is configured correctly for the db as it is creating the sqlite db, and the necessary folders

settings.py

import os
*
* configure connection do db, etc
*

ROOT_URLCONF = 'blog.urls'

TEMPLATE_DIRS = (
    # 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.
    os.path.join(os.path.dirname(__file__),'templates'), # creates a absolute path
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

urls.py

from django.conf.urls.defaults import patterns, include, url

# Uncomment the next two lines to enable the admin:
 from django.contrib import admin
 admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'blog.views.home', name='home'),
    # url(r'^blog/', include('blog.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    (r'^, 'blog.views.index'), #<------------------- index/root entry
    # Uncomment the next line to enable the admin:
     url(r'^admin/', include(admin.site.urls)),


)

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>My Blog</title>
    </head>

<body>
    <H1>Test Django</H1>
    <div id="content">
    {% block content %}
    {% endblock %}
    </div>
</body>

</html> 

So what im i missing?

so i can see my index html with data [as now it shows the tags {% block content %} {% endblock %} and no data in them

and cannot have acces to http://mako34.alwaysdata.net/blog/admin/

thanks!

manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216

1 Answers1

2

Your URLs should look like this:

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

Note that the index URL you want to work is now wrapped in a url() call. Also, the index URL now follows the admin URL so that urls referencing /admin are handled by the admin URL, not the catch all index URL you've defined.

URL handlers work on first match. Your url(r'^') matches EVERYTHING, so doesn't give a chance for the admin url to work. You should probably change it to url(r'^$') which will match a 'no-path' URL, not a 'every url'. Note the addition of the $ sign, marking the end of a pattern.

Edit:

Ok, now I understand your problem better. What you're trying to do, is to deploy a django app at a particular server path that requires a prefix in the URL path.

This is what is usually a standard URL:

http://www.example.com/
http://www.example.com/admin/
http://www.example.com/index/

Instead, this is what you're trying to do:

http://www.example.com/myapp/
http://www.example.com/myapp/admin/
http://www.example.com/myapp/index/

Django usually expects your app to be deployed at the root URL, with no path. The path is used to find which internal django app should handle the request.

There are two ways for you to solve your problem. The first, and what I'd consider the correct way, is to use the sites framework as described here.

The other, is to add the prefix to all of your URLs in urlpatterns like so:

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

But you will also need to remember to add your 'blog' prefix to several settings that expect URLs like LOGIN_REDIRECT etc etc.

What you really should do though, is make django work at the URL: mako34.alwaysdata.net and forget the /blog/ altogether, but modifying apache to redirect all requests to mod_wsgi.

Community
  • 1
  • 1
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
  • hi, thanks, did the proposed changes, but still not showing index.html (http://mako34.alwaysdata.net/blog/) properly, and on /admin (http://mako34.alwaysdata.net/blog/admin/), shows Not found,, The requested URL /blog/admin/ was not found on this server. – manuelBetancurt Mar 24 '12 at 09:57
  • @MaKo the url you should be hitting for admin is mako34.alwaysdata.net/admin. The URL for your blog should be mako34.alwaysdata.net. Give those a crack – Josh Smeaton Mar 24 '12 at 10:04
  • thanks for your response, so whould the urlpatterns, be like this?? url(r'^admin/', include(mako34.alwaysdata.net/admin)), url(r'^', 'mako34.alwaysdata.net'), ;;; as I tried this and still not working,, or should it be different?, thanks! – manuelBetancurt Mar 24 '12 at 10:08
  • ok thanks!, will try that solution, but just to keep it simple and if I do it from the root, /home/mako34, I can see this folders: admin cgi-bin www .. so shall i create the project "django-admin.py startproject blog" in this root folder?, i was placing this project inside www thanks! – manuelBetancurt Mar 24 '12 at 10:30
  • Place it inside /www/ where you had it before. But make sure your webserver is passing all traffic directly to django. The current index page up on your page should not be visible, because django should be serving all traffic (except static content which you can figure out later). – Josh Smeaton Mar 24 '12 at 10:36