简体   繁体   中英

Django ImportError at / no matter what I do

So I've just started playing around with Django and I decided to give it a try on my server. So I installed Django and created a new project, following the basics outlined in the tutorial on Djangoproject.com

Unfortunatly, no matter what I do, I can't get views to work: I constantly get

ImportError at /

No module named index

Here is a screenshot of this error

I've been googling and trying various commands with no luck, and I'm literally about to tear my hair out until I become bald. I've tried adding the django source directory, my project directory, and the app directory to PYTHONPATH with no luck. I've also made sure that init .py is in all of the directories (both project and app) Does anyone have any idea as to what could be going wrong here?

UPDATES

Sorry, I was in kind of a rush while posting this, here's some context:

The server I've been trying is just django's built in server using manage.py (python manage.py 0.0.0.0:8000, since I need to access it externally) on linux (debian)

appdir/views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("Sup")

def test(request):
    return HttpRespons("heyo")

urls.py

from django.conf.urls.defaults import *

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

urlpatterns = patterns('',
    # Example:
    # (r'^****/', include('****.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
    # to INSTALLED_APPS to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
    (r'^test/', include('mecore.views.test')),
    (r'^', include('mecore.views.index'))
)

Your urls.py is wrong; you should consider reading this and this .

You don't include a function; you include a module. You name a function, mecore.views.index . You only include entire modules include('mecore.views') .

from django.conf.urls.defaults import *

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

urlpatterns = patterns('',
    # Example:
    # (r'^****/', include('****.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
    # to INSTALLED_APPS to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
    (r'^test/', 'mecore.views.test'),
    (r'^', 'mecore.views.index')
)

Do you have __init__.py in each of the mecore and views directories, as well as an index.py in views?

A directory is a package, from Python's viewpoint, only if it has a file named __init__.py (it may be empty, if you don't need to execute any special code when that package is imported, but it has to be there).

Edit: note that in include you must name the python path to a module, not to a function: see Django's relevant docs -- judging from your comment you appear to be mis-using include , as I see @S.Lott had surmised in his answer.

From ImportError No module named views :

Try and move views.py to the "inner" mysite directory. Views are part of an application, hence the need to move them inside the application directory (and not in the project directory).

The error message you get indicates that mysite (the application) has no views.py module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM