python - Elementary, unresolved import from my_app.views in Django (PyDev) -


my project tree in home/djangoprojects/django_bookmarks/env/django_bookmarks looks like:

django_bookmarks/ #project     django_bookmarks/         __init__.py         settings.py         urls.py         wsgi.py     bookmarks/ #made python manage.py startapp bookmarks         __init__.py         models.py         test.py         views.py     manage.py 

in ../bookmarks/views have:

from django.http import httpresponse  def main_page(request):     output = '''     <html>     <head><title>%s</title></head>     <body>     <h1>%s</h1><p>%s</p>     </body>     </html>     ''' % (     'django bookmarks',     'welcome django bookmarks',     'where can store , share bookmarks!'     )     return httpresponse(output) 

in .../django_bookmarks/urls have:

from django.conf.urls import patterns, include, url bookmarks.views import main_page # unresolved import: main_page # bookmarks.views import * # undefined variable: main_page in line below  ...  urlpatterns = patterns('',                        (r'^$', main_page) ) 

edit:

in ../django_bookmarks/settings:

    # django settings django_bookmarks project.  debug = true template_debug = debug  admins = (     # ('your name', 'your_email@example.com'), )  managers = admins  databases = {     'default': {         'engine': 'django.db.backends.sqlite3', # add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.         'name': '/home/novak/djangoprojects/django_bookmarks/env/django_bookmarks/django_bookmarks/sqlite.db',                      # or path database file if using sqlite3.         # following settings not used sqlite3:         'user': '',         'password': '',         'host': '',                      # empty localhost through domain sockets or '127.0.0.1' localhost through tcp.         'port': '',                      # set empty string default.     } }  # hosts/domain names valid site; required if debug false # see https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts allowed_hosts = []  # local time zone installation. choices can found here: # http://en.wikipedia.org/wiki/list_of_tz_zones_by_name # although not choices may available on operating systems. # in windows environment must set system time zone. time_zone = 'america/chicago'  # language code installation. choices can found here: # http://www.i18nguy.com/unicode/language-identifiers.html language_code = 'en-us'  site_id = 1  # if set false, django make optimizations not # load internationalization machinery. use_i18n = true  # if set false, django not format dates, numbers , # calendars according current locale. use_l10n = true  # if set false, django not use timezone-aware datetimes. use_tz = true  # absolute filesystem path directory hold user-uploaded files. # example: "/var/www/example.com/media/" media_root = ''  # url handles media served media_root. make sure use # trailing slash. # examples: "http://example.com/media/", "http://media.example.com/" media_url = ''  # absolute path directory static files should collected to. # don't put in directory yourself; store static files # in apps' "static/" subdirectories , in staticfiles_dirs. # example: "/var/www/example.com/static/" static_root = ''  # url prefix static files. # example: "http://example.com/static/", "http://static.example.com/" static_url = '/static/'  # additional locations of static files staticfiles_dirs = (     # put strings here, "/home/html/static" or "c:/www/django/static".     # use forward slashes, on windows.     # don't forget use absolute paths, not relative paths. )  # list of finder classes know how find static files in # various locations. staticfiles_finders = (     'django.contrib.staticfiles.finders.filesystemfinder',     'django.contrib.staticfiles.finders.appdirectoriesfinder', #    'django.contrib.staticfiles.finders.defaultstoragefinder', )  # make unique, , don't share anybody. secret_key = '0i_*)b7)hc0oy^7qyit47w%k65pwfo7e@p)k=7lho3)q5!mx+!'  # list of callables know how import templates various sources. template_loaders = (     'django.template.loaders.filesystem.loader',     'django.template.loaders.app_directories.loader', #     'django.template.loaders.eggs.loader', )  middleware_classes = (     'django.middleware.common.commonmiddleware',     'django.contrib.sessions.middleware.sessionmiddleware',     'django.middleware.csrf.csrfviewmiddleware',     'django.contrib.auth.middleware.authenticationmiddleware',     'django.contrib.messages.middleware.messagemiddleware',     # uncomment next line simple clickjacking protection:     # 'django.middleware.clickjacking.xframeoptionsmiddleware', )  root_urlconf = 'django_bookmarks.urls'  # python dotted path wsgi application used django's runserver. wsgi_application = 'django_bookmarks.wsgi.application'  template_dirs = (     # put strings here, "/home/html/django_templates" or "c:/www/django/templates".     # use forward slashes, on windows.     # don't forget use absolute paths, not relative paths. )  installed_apps = (     'django.contrib.auth',     'django.contrib.contenttypes',     'django.contrib.sessions',     'django.contrib.sites',     'django.contrib.messages',     'django.contrib.staticfiles',     # uncomment next line enable admin:     # 'django.contrib.admin',     # uncomment next line enable admin documentation:     # 'django.contrib.admindocs', )  # sample logging configuration. tangible logging # performed configuration send email # site admins on every http 500 error when debug=false. # see http://docs.djangoproject.com/en/dev/topics/logging # more details on how customize logging configuration. logging = {     'version': 1,     'disable_existing_loggers': false,     'filters': {         'require_debug_false': {             '()': 'django.utils.log.requiredebugfalse'         }     },     'handlers': {         'mail_admins': {             'level': 'error',             'filters': ['require_debug_false'],             'class': 'django.utils.log.adminemailhandler'         }     },     'loggers': {         'django.request': {             'handlers': ['mail_admins'],             'level': 'error',             'propagate': true,         },     } } 

in ../django_bookmarks/wsgi:

""" wsgi config django_bookmarks project.  module contains wsgi application used django's development server , production wsgi deployments. should expose module-level variable named ``application``. django's ``runserver`` , ``runfcgi`` commands discover application via ``wsgi_application`` setting.  have standard django wsgi application here, might make sense replace whole django wsgi application custom 1 later delegates django one. example, introduce wsgi middleware here, or combine django application application of framework.  """ import os  # defer django_settings_module in environment. breaks # if running multiple sites in same mod_wsgi process. fix this, use # mod_wsgi daemon mode each site in own daemon process, or use # os.environ["django_settings_module"] = "django_bookmarks.settings" os.environ.setdefault("django_settings_module", "django_bookmarks.settings")  # application object used wsgi server configured use # file. includes django's development server, if wsgi_application # setting points here. django.core.wsgi import get_wsgi_application application = get_wsgi_application()  # apply wsgi middleware here. # helloworld.wsgi import helloworldapplication # application = helloworldapplication(application) 

django version 1.5.1. when run project (ctrl+f11) there no error in console, in-line error markers in eclipse text editor. eclipse apparently somehow runs previous version of code works , in browser shows previous version ignoring error. earlier same code works (this code book) , therefore think problem settings in eclipse or in way created project. created project on way http://blog.bixly.com/post/25093181934/setting-up-eclipse-for-python-django-development

i read here people had problems importing modules , because lack of init.py in app directory or issue pythonpath settings. read did not me solve problem.

*i'm begginer in django , first time write django. example book "learning website development django", ayman hourieh.

you need inform python interpreter 1 directory above.

try relative path import

from ..bookmarks.views import main_page 

the '..' says directory above find bookmarks package.

may suggest more robust pattern accomplish this?

django_bookmarks/ #project django_bookmarks/     __init__.py     settings.py     urls.py     wsgi.py bookmarks/ #made python manage.py startapp bookmarks     __init__.py     models.py     test.py     views.py     urls.py # add urls.py bookmarks app manage.py 

in django_bookmarks/urls.py

from django.conf.urls import patterns, include, url # bookmarks.views import main_page # remove    # directs django urls.py within bookmarks app urlpatterns = patterns('',     (r'^$', include('bookmarks.urls')) ) 

in bookmarks/urls.py

from django.conf.urls import patterns, include, url bookmarks import views    # directs django urls.py within bookmarks app urlpatterns = patterns('',     (r'^$', views.main_page)     # can add more bookmark urls match bookmark views ) 

this pattern more maintainable , allows bookmark url patterns live inside bookmarks/urls.py.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -