2
votes

I am building a site with django and cannot import views into my URL file.

My URL file:

from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
]

Error: ImportError: cannot import name 'views' from 'blog' (..\blog\blog__init__.py)

If I try "from blog import views", I get the same error.

If I try "from blogapp import views" (blogapp is the name of the app within the blog foler), I get:

RecursionError: maximum recursion depth exceeded while calling a Python object.

Essentially, it bounces back and forth between line 23 in check_resolver (django\core\checks\urls.py) and line 397 in check (django\urls\resolvers.py).

If I try just: "import views", I get "ModuleNotFoundError: No module named 'views'"

My project structure: Main directory is "blog", contains two folders (blog and blogapp) as well as the db.sqlite3 and manage.py files.

Subfolder blog contains pycache folder, and these files: init.py, settings.py, urls.py, wsgi.py.

Subfolder blogapp contains pycache folder migrations folder, and these files: init.py, admin.py, apps.py, models.py, tests.py, views.py.

Both blog and blogapp are in my INSTALLED_APPS in settings.py.

I checked several similarly named questions (and their suggested answers) and could not figure out what is going wrong.

1

1 Answers

1
votes

First rename your project folder to something different than the app folder such as blog-project. Then you should have an updated tree as follows:

blog-project
-blog (settings.py, urls.py, and wsgi.py in this folder)
--urls.py (See My Example 1)
-blogapp
--templates
---blogapp
----blogapp_home.html
----index.html
--urls.py (In this file you do from . import views)

When you navigate to the home page Django reads from your root urls.py. Since you have your starting point set to:


path('', include('blogapp.urls')),

It then loads the urls.py from the new application you created, blogapp. From urls.py in the blogapp folder import your views.


# Example 1 blog-project/blog/urls.py

from django.contrib import admin
from django.urls import path, include
from blogapp import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='home'),
    path('blogapp/', include('blogapp.urls')),
]
# Example 2 blog-project/blogapp/urls.py

from django.urls import path, include
from . import views

urlpatterns = [

    path('', views.blogapphome, name='blogapp-home'),

]
# Example 2 Views blog-project/blogapp/views.py

from django.shortcuts import render

def index(request):
    sometext = 'More Text'
    context = {'text': sometext}

    return render(request, 'blogapp/index.html', context)

def blogapphome(request):
    sometext = 'Some Text Here'
    context = {'text': sometext}

    return render(request, 'blogapp/blogapp_home.html', context)
# blog-project/blog/settings.py

INSTALLED_APPS = [
    'blogapp.apps.BlogappConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]