I am starting a very basic Django project (using PyCharm IDE in the process). This is what I do initially.
cd
into my workspace
directory and run django-admin.py startproject mysite
. This creates the following directory structure: Run cd mysite
, followed by python manage.py runserver
. This works as expected, and I am able to view the standard localhost served page in my browser.
Now I need to add another directory called customviews
inside mysite
, and inside that add a file called myview.py
with a basic method that returns a HTTPResponse
object. Now the folder structure is this:
URL
to this view, so I head off to the urls.py
module, and try to import the method in the view I just created. It shows as unimportable. So I add the path to the PyCharm IDE specified Python path here and mark mysite
as src folder: from myview import current_datetime
. However, the command line, where I started the server in the 2nd step throws error, and consequently the mapping fails and so does the page I want to load Please help me fix this error!
PS I have previously worked with Django on Eclipse, I never faced this kind of issue. This is my urls.py
code:
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import *
from myview import current_datetime
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
Can you please us the code of the file where you want to import the view? I think you have missed to create a __init__.py
inside the customviews folder?
You shouldn't add that directory to the path.
You have two issues: firstly, as adriansq points out, you need an empty __init__.py
in that directory. And secondly, it is in the wrong place; it should be one level higher, directly under the outer mysite
directory.
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.