0

I have created a urls and views folder, like templates, but it is not working. I am getting error: Exception Value: No module named urls

Here's what my project looks like:

requirements.txt:

Django==1.6.5
pymongo==2.8

.

├── DjangoProject
│   ├── DjangoProject
│   │   ├── app
│   │   │   ├── __init__.py
│   │   │   ├── __init__.pyc
│   │   │   ├── models.py
│   │   │   ├── models.pyc
│   │   │   ├── templates
│   │   │   ├── tests.py
│   │   │   ├── urls
│   │   │   │   └── test.py
│   │   │   └── views
│   │   │       └── test.py
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── settings.py
│   │   ├── settings.pyc
│   │   ├── urls.py
│   │   ├── wsgi.py
│   │   └── wsgi.pyc
│   └── manage.py
└── requirements.txt

settings.py file :

INSTALLED_APPS = (
    'DjangoProject.app',
     ........
)
ROOT_URLCONF = 'DjangoProject.app.urls'

DjangoProject/urls.py :

urlpatterns = patterns('',
    url(r'^$', include('DjangoProject.app.urls')),  
    url(r'^admin/', include(admin.site.urls)),
)

app/urls/test.py :

from django.conf.urls import patterns, url
urlpatterns = patterns('DjangoProject.app.views.test',
        url(r'^/create/$','first_view',name='first_view'),
)

app/views/test.py :

from django.shortcuts import render, render_to_response
from django.http import HttpResponse
from django.template import loader
from django.template import RequestContext

def first_view(request):
    return HttpResponse("Hi")
SH7890
  • 545
  • 2
  • 7
  • 20
ashutosh
  • 1
  • 4
  • I don't understand what you've called your url and views files. Are they really both called test.py, inside directories called urls and views? If so why? – Daniel Roseman Jul 14 '17 at 13:50
  • yes,both called test.py, inside directories called urls and views. my plan is to create one major app and inside that different apps which gets call by links, like webmaster. will __init__.py will help me for this? – ashutosh Aug 08 '17 at 06:12
  • Well, don't. Move those files into the app directory and call them what they are, urls.py and views.py. – Daniel Roseman Aug 08 '17 at 06:49

3 Answers3

0

Since you are trying to make it work like a module try adding __init__.py in both of url and views folder with the following content

from .test import *

To explain it further in terms of your question __init__.py will be placed in the DjangoProject/DjangoProject/app/urls/ and DjangoProject/DjangoProject/app/views/.

To use ROOT_URLCONF = 'DjangoProject.app.urls' you will have to import everything into the __init__.py file as well.

For the use case of importing views as seen here.

from django.conf.urls import patterns, url
urlpatterns = patterns('DjangoProject.app.views.test',        
    url(r'^/create/$','first_view',name='first_view'),
)

you don't even have to import anything in the __init__.py of views.

pyvkd
  • 96
  • 5
  • http://pythoncentral.io/package-python-django-application-reusable-component/ OR https://110j.wordpress.com/2009/06/17/organize-django-applications/ – ashutosh Aug 22 '17 at 06:59
  • the above two is link i found. i want to create module(folder) for views and urls for one major app. i understood that __init__.py will help me this but, i dont know how? will you elaborate this to me? and also is my ROOT_URLCONF in settings.py and url in DjangoProject/urls.py is right? – ashutosh Aug 22 '17 at 07:05
  • the `__init__.py` file will go into every folder which you want to use as module. So in this case it will go into `DjangoProject/DjangoProject/app/urls/__init__.py` and similarly for views folder. also you will have to import things into the `__init__.py` that you want to access at the module level. – pyvkd Aug 23 '17 at 07:41
  • you are right...thank you mr. pyvkd. simply genius. you save me from drowning. i am in the air~~~ – ashutosh Aug 24 '17 at 05:40
0

The idea from django architecture is to have a file for urls and views for every app. Remember that urls.py stores all the urls from the app, if you want to extend it, try creating another app. What I suggest is removing that folder with both tests.py and structuring the file directly into your DjangoApp folder.

Also, as the comment above says correctly, for python to treat a folder like a module you should create a file called __init__.py, check out What is __init__.py for?.

Agustin
  • 95
  • 2
  • 10
0

Your application file structure and your configuration has problems. read this: https://docs.djangoproject.com/en/1.11/intro/tutorial01/

for instance: You should assign your function in view.py to its url so in this line:

url(r'^/create/$','first_view',name='first_view'),

change it to:

url(r'^/create/$', views.first_view, name='first_view'),
pooya
  • 155
  • 6