Adding an Application to a Hosted Django Project

I have poured many times on the site and have not found a solution that works for me. Time listener, first time caller.

I have an existing Django application (based on the Django sample documentation site, including polls). I moved this and ran it on the web server via wsgi.

I created a new local project of my own content. I did it the same way as an example site with an application at the same level as the main application of the project.

Now I am trying to add the application that I created to the sample site. I am trying to reflect the organization and plumbing for the polls app, as it connects to mysite.

My file structure is as follows:

  • passenger_wsgi.py
  • Mysite
    • mysite (main application)
      • __init__.py
      • settings.py
      • urls.py
      • wsgi.py
    • polls (example site application)
      • __init__.py
      • urls.py
      • views.py
      • models.py
    • Kernel (the application I am adding)
      • __init__.py
      • urls.py
      • views.py
      • models.py
    • manage.py

Adding settings is as follows:

 INSTALLED_APPS = { 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'polls', 'core' } 

MySite / urls.py:

 from django.conf.urls import patterns, include, url from django.contrib import admin urlpatterns = patterns('', url(r'^$', 'polls.views.home', name='home2'), url(r'^polls/', include('polls.urls', namespace="polls")), url(r'^admin/', include(admin.site.urls)), url(r'^core/', include('core.urls', namespace="core")), ) 

kernel /urls.py:

 from django.conf.urls import patterns, include, url from django.contrib import admin #from core import views urlpatterns = patterns('', url(r'^', 'core.views.home', name='home'), ) 

When I download "manage.py shell", I can import the URLs, models, and view everything from the kernel. I can even use django.test.Client to get "/ core /", and I get a 200 response with the expected content. However, when I try to go to the site (any URL) in the browser, I get: "ImportError at No module named urls"

If I remove the call by including core.urls, the other URLs will work as expected.

I tried the number if different settings for both urls.py files, including calling the kernel .views calling directly from the top urls.py, ignoring core.urls, resulting in "No module named views".

Any advice would be greatly appreciated. I also welcome tips on how to write better questions.

Stack trace

Change I also wanted to mention that I tried to delete all compiled python files in order to exclude the possibility of unreasonable logic.

Change 2 . It works in virtualenv. I installed a newer version of Python on the Dreamhost server.

passenger_wsgi.py:

 import sys, os cwd = os.getcwd() sys.path.append(cwd) sys.path.append(cwd + '/mysite') if sys.version < "2.7.9": os.execl(cwd + "/env/bin/python", "python2.7", *sys.argv) sys.path.insert(0, cwd + '/env/bin') sys.path.insert(0, cwd + '/env/lib/python2.7') sys.path.insert(0, cwd + '/env/lib/python2.7/site-packages/django') sys.path.insert(0, cwd + '/env/lib/python2.7/site-packages') os.environ['DJANGO_SETTINGS_MODULE'] = "mysite.settings" from django.core.wsgi import get_wsgi_application application = get_wsgi_application() 

MySite / MySite / wsgi.py:

 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application() 

Edit 3 : here core / views.py

 from django.shortcuts import render from django.core.mail import send_mail from django.http import HttpResponseRedirect # Create your views here. def home(request): return render(request, 'core/home.html') 

Change 4 . Sample GET header from browser. Results in 500 Import Error; No module named url

 GET /core/ HTTP/1.1 Host: #redacted Connection: keep-alive Pragma: no-cache Cache-Control: no-cache Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8 Cookie: csrftoken=36HpckIqzOMbw29BOrQWcnaIJbMn6pwo; sessionid=z9c58vb6610kevo4yytolcbhlaqsoqnm 

Change 5 . Since the answer basically showed that everything looks right, I expanded the scope of the question to include Dreamhost and sent a support ticket with them. I will let you know here if any of this comes out.

+5
source share
3 answers

I finally found a resolution.

I tried to create a completely new project and add a โ€œmainโ€ application, but I saw the same problem. It became clear to me that the reason he was unable to access the submodules (views, models, URLs) was because the "core" module loaded by the passenger was not the kernel module that I created.

I tried changing the name of the application and the problem was resolved.

I noticed that when I try to create an application called "test", Django gives you a message that it is not allowed due to the existing module. Apparently, this is not the case in all directions.

To continue, I looked at the passenger_wsgi.py file (also in the question):

 import sys, os cwd = os.getcwd() sys.path.append(cwd) sys.path.append(cwd + '/mysite') if sys.version < "2.7.9": os.execl(cwd + "/env/bin/python", "python2.7", *sys.argv) sys.path.insert(0, cwd + '/env/bin') sys.path.insert(0, cwd + '/env/lib/python2.7') sys.path.insert(0, cwd + '/env/lib/python2.7/site-packages/django') sys.path.insert(0, cwd + '/env/lib/python2.7/site-packages') os.environ['DJANGO_SETTINGS_MODULE'] = "mysite.settings" from django.core.wsgi import get_wsgi_application application = get_wsgi_application() 

Looking back, I see an error. In the following DreamHost configuration guide for alternate versions of python (found here ), I inserted my python path for env at the beginning of sys.path . This causes the django.core module to be mapped before my project application, which was added at the end of sys.path .

The moral of history is to know the names of the modules and the construction of the path.

0
source

URLs look wrong in core / urls.py instead of doing

 urlpatterns = patterns('', url(r'^', views.home, name='home'),) 

to try

  urlpatterns = [url(r'^','core.views.home',name='home'),] 

PS Name views.name specified

0
source

Just guess, but try replacing url(r'^core/', include('core.urls', namespace="core")) with (r'^core/', include('core.urls')) in mysite /urls.py.

0
source

Source: https://habr.com/ru/post/1213183/


All Articles