Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import name 'views',.Python, Django

Tags:

python

django

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

like image 511
Maddie Graham Avatar asked Oct 31 '25 03:10

Maddie Graham


1 Answers

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
like image 69
Druta Ruslan Avatar answered Nov 02 '25 16:11

Druta Ruslan