Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am beginner in django and following a tutorial. got import error even after typing the exact code. here is the code

views.py:

from django.shortcuts import render
def index(request):
    return render(request, 'learning_logs/index.html')

urls.py:

from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from  . import views
urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'',include('learning_logs.urls', namespace ='learning_logs')),
    url(r'^$', views.index, name='index'),

app urls:

from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from learning_log import views
app_name = 'learning_logs'
urlpatterns = [
    #HomePage
    url(r'^$', views.index, name='index'),
    url(r'^topics/$',views.topics, name='topics'),
    # Detail page for a single topic
    url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic'),
    url(r'^new_topic/$', views.new_topic, name='new_topic'),

error

ImportError: cannot import name 'views' from 'learning_log' (C:\Users\hhh\PycharmProjects\learning_log\learning_log\learning_log\__init__.py)
                You probably skipped through the part where urls.py is created for the app. The urls.py you shared here belongs to project, and with include statement, you must include urls.py of your app. Then in the app's urls.py you can import views.
– Alp
                Jul 25, 2020 at 20:54

This is happening due to that the current folder don't have any views.py file.

from . import views 

This means you are looking for views.py in current folder which isn't available right now. You can try this:

from your_appname import views
                @Batoota if that's not what you are looking for, then kindly share your directory screenshots ;)
– Mubashar Javed
                Jul 26, 2020 at 4:53
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.