没有名为urls错误的模块

我有一个简单的管理django项目,在那里我创建了另一个名为webadmin的应用程序。 项目结构如下:

  • tstprj
  • manage.py
  • tstprj
  • settings.py
  • urls.py
  • wsgi.py
  • init.py
  • WebAdmin的
  • admin.py
  • urls.py
  • views.py
  • models.py
  • init.py
  • 我有一个单独的wsgi文件,我从apache conf中引用:

    # tstprj.wsgi
    import os
    import sys
    sys.path.insert(0, '/var/www/tstprj')
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'tstprj.settings'
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    

    和apache的虚拟主机 - tstprj.conf:

    <VirtualHost *:80>
    WSGIScriptAlias / /home/pm/dev/tstprj.wsgi
    
    ServerName <<my_ip_address>>
    
    Alias /static /var/www/tstprj/static/
    
    <Directory /var/www/tstprj/>
    AllowOverride All
    Order allow,deny
    Allow from all
    </Directory>
    </VirtualHost>
    

    在/ No模块命名为urls时,出现错误ImportError。 追溯:

    
        Environment:
    
    
        Request Method: GET
        Request URL: http://<<my_ip_address>>/
    
        Django Version: 1.6
        Python Version: 2.7.3
        Installed Applications:
        ('django.contrib.admin',
         'django.contrib.auth',
         'django.contrib.contenttypes',
         'django.contrib.sessions',
         'django.contrib.messages',
         'django.contrib.staticfiles',
         'webadmin')
        Installed Middleware:
        ('django.contrib.sessions.middleware.SessionMiddleware',
         'django.middleware.common.CommonMiddleware',
         'django.middleware.csrf.CsrfViewMiddleware',
         'django.contrib.auth.middleware.AuthenticationMiddleware',
         'django.contrib.messages.middleware.MessageMiddleware',
         'django.middleware.clickjacking.XFrameOptionsMiddleware')
    
    
        Traceback:
        File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
          101.                 resolver_match = resolver.resolve(request.path_info)
        File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
          318.             for pattern in self.url_patterns:
        File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
          346.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
        File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
          341.             self._urlconf_module = import_module(self.urlconf_name)
        File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
          40.         __import__(name)
        File "/var/www/tstprj/tstprj/urls.py" in 
          11.     url(r'^admin/webadmin', include('webadmin.urls')),
        File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py" in include
          26.         urlconf_module = import_module(urlconf_module)
        File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
          40.         __import__(name)
    
        Exception Type: ImportError at /
        Exception Value: No module named urls
    
    

    而且,当然,tstprj.urls:

    
        from django.conf.urls import patterns, include, url
    
        from django.contrib import admin
        admin.autodiscover()
    
        urlpatterns = patterns('',
    
            url(r'^admin/webadmin', include('webadmin.urls')),
            url(r'^admin/', include(admin.site.urls)),
        )
    
    

    编辑:webadmin.urls

    from django.conf.urls import *
    from webadmin.views import *
    
    urlpatterns = patterns('',  
        url(r'^/playlist/(?P<plid>(d+))/song/add/$', addSong, name="addsong"),
        url(r'^/playlist/(?P<plid>(d+))/song/(?P<sgid>(d+))/delete/$', deleteSong),
        url(r'^/playlist/(?P<plid>(d+))/up/$', moveUpSong),
        url(r'^/playlist/(?P<plid>(d+))/down/$', moveDownSong),
        url(r'^/playlist/(?P<plid>(d+))/top/$', moveTopSong),
        url(r'^/playlist/(?P<plid>(d+))/bottom/$', moveBottomSong),
        url(r'^/playlist/(?P<plid>(d+))/position/$', moveToPositionSong),
        url(r'^/playlist/(?P<plid>(d+))/deleteSelected/$', deleteSelectedSongs),
        url(r'^/user/(?P<uid>(d+))/deleteSelected/$', deleteSelectedStations), 
        url(r'^/user/(?P<uid>(d+))/station/(?P<sid>(d+))/playlist/deleteSelected/$', deleteSelectedPlaylists),
        url(r'^/user/(?P<uid>(d+))/station/(?P<sid>(d+))/playlist/(?P<plid>(d+))/delete/$', deletePlaylist),
        url(r'^/user/(?P<uid>(d+))/station/(?P<sid>(d+))/playlist/add/$', addPlaylist, name="addplaylist"),   
        url(r'^/user/(?P<uid>(d+))/playlist/all/$', playlist_list, name="playlist_list"),  
        url(r'^/user/(?P<uid>(d+))/station/add/$', addStation, name="addStation"),
        url(r'^/user/(?P<uid>(d+))/station/(?P<sid>(d+))/$', viewStation, name="viewStation"),
        url(r'^/user/(?P<uid>(d+))/station/(?P<sid>(d+))/delete/$', deleteStation, name="deleteStation"), 
    )
    

    有什么事?


    我不确定,但你的apache配置可能不完整。 我不确定它所称的Python版本是你想要的。

    我通常设置python路径,如下所示:

    WSGIDaemonProcess process_name processes=2 threads=12 python-path=/home/pm/dev:home/pm/.../lib/python2.7
    WSGIProcessGroup process_name
    WSGIRestrictEmbedded On
    WSGILazyInitialization On
    WSGIScriptAlias / /home/pm/dev/tstprj.wsgi
    

    第一行确保Python使用安装在“home / pm /.../ lib / python2.7”中的库,并且能够到达“/ home / pm / dev”。

    我不确定这个问题的答案,但也许你可以尝试。


    当然你不想要这个答案了! 但:
    将项目和应用程序目录添加到项目的wsgi.py中的sys.path中,它可以找到项目中的所有模块

    import sys
    sys.path.append('<Project_Directory>')
    sys.path.append('<Project_Directory>/AppName')
    
    链接地址: http://www.djcxy.com/p/40571.html

    上一篇: no modules named urls error

    下一篇: Mezzanine ImportError when running tests