4

I have read many answers in this forum, but they does not solve my problem. I will be very grateful for help.

My file views.py returns this error:

from . import views
ImportError: cannot import name 'views' from '__main__' (C:/Users/tymot/Desktop/weather app/env/Environemnt/the_weather/weather/views.py)

views.py (Environemnt\the_weather\weather)

from django.shortcuts import render
from django.contrib import admin

def index(request):
    return render(request, 'weather/index.html') #returns the index.html 

urls.py (Environemnt\the_weather\weather)

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index),  #the path for our index view
]

urls.py (Environemnt\the_weather\the_weather)

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

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

templates(the_weather\weather\templates\weather) only file index.html

Directory

-the_weather
--the_weather
---__init__
---setting
---urls
---wsgi
--weather
---migrations
----__init__
---templates
----weather
-----index
---__init__
---admin
---apps
---models
---tests
---urls
---views
--db
--manage.py

I try use to resolved my problem from __future__ import absolute_import, or homepage import views. I else try copy views.py to directory templates (and modify its code) but unfortunately it not work

3
  • You write urlpatterns in views.py? Commented Jul 13, 2018 at 14:33
  • what version of Python are you using? Commented Jul 13, 2018 at 15:14
  • Python 3.7 Django 1.X Commented Jul 13, 2018 at 15:18

10 Answers 10

5

You need to separate your views and urls create a new module (file) urls.py in your app, in your case it is weather folder, and add these code there, and remove it from views.py, you can read here about it to understand it better.

Path : the_weather/weather/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index),  #the path for our index view
]

Path : the_weather/weather/views.py

from django.shortcuts import render
from django.contrib import admin

def index(request):
    return render(request, 'weather/index.html') #returns the index.html template
Sign up to request clarification or add additional context in comments.

10 Comments

My views now: from django.shortcuts import render from django.contrib import admin def index(request): return render(request, 'weather/index.html') My urls.py in weather now: from django.urls import path from . import views urlpatterns = [ path('', views.index), #the path for our index view but unfortunately it still does not work.
i update my answer try it, i wrote the filepath where you need to put your code
But these ImportError: cannot import name 'views' from '__main__' i see now in my urls.py file.
can you edit your answer, and put there all traceback ?
try to use from weather import views
|
1

You would need to put your views.py file in your weather folder. Make sure it is in the right weather folder, I am guessing you have two weather folders. Also make sure these codes are right;

#Path : the_weather/weather/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index),  #the path for our index view
]

#Path : the_weather/weather/views.py

from django.shortcuts import render
from django.contrib import admin

def index(request):
    return render(request, 'weather/index.html') #returns the index.html template

Comments

0

Why don't you try like this

from .views import index

2 Comments

i try use it in my url.py . Answer from PyCharm is No module named '__main__.views'; '__main__' is not a package
Could you please share the screenshots of your code and error traceback... both important..
0

There may be separate folder of urls and views.py what you have to do is you have to write from appname import views suppose your app name is myapp you have have to write from myapp import views

Comments

0

use should create new url.py in weather and write code in views.py

url.py

from django.urls import path
from . import views
urlpatterns = [
   path('', views.index, name='index')
]

write code in urls.py of your project

from django.urls import path, include
from . import views
urlpatterns = [
   path('wheather/',include('wheather'))
]

Comments

0

I had Similar problem, it didn't solved, till I added an empty views.py in root in example, in your case add to Environemnt\the_weather\the_weather

Comments

0

I had the same problem.

click for image

click for image

In formsdemo\formsdemo\__init__.py, I changed the import code to from forms app import views.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0
from django.contrib import admin
from django.urls import path
import main.views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', main.views.home_page, name="home_page"),
]
 

Comments

0

Well, i was also facing the same issue. I was continuously getting server errors but then I figured out it was a simple error.

go to the urls.py file

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name = index),  #the path for our index view
]

I just had to remove name = index and the server worked with 0 errors.

enter image description here

Comments

0

You can try this:

from formsApp(folder containing view file) import views

from views import *

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.