AttributeError: module 'PrivateSchools.views' has no attribute 'HomePage' - python

I am getting the error when try to run the server.
File"C:\Users\admin\Desktop\InstitutionFinderWebsite\
InstitutionFin
derWebsite\urls.py", line 26, in <module>path('HomePage/',
views.HomePage),
AttributeError: module 'PrivateSchools.views' has no attribute
'HomePage'
I had imported all the views from the three apps as below
from django.conf.urls import include
from django.contrib import admin
from django.urls.conf import path
from HomePage import views
from PublicSchools import views
from PrivateSchools import views
On the urls.py have tried the 2 methods below but the are not
all working.
Method one here i used views. to map the urls.
urlpatterns = [
path('admin/', admin.site.urls),
path('HomePage/', views.HomePage),
path('PublicSchools/', views.PublicSchools),
path('PrivateSchools/', views.PrivateSchools),
]
This is method two in trying to solve it by trying to give the
names.
urlpatterns = [
path('admin/', admin.site.urls),
path('HomePage/', views.HomePage, name='PrivateSchools'),
path('PublicSchools/', views.PublicSchools,
name='PublicSchools'),
path('PrivateSchools/', views.PrivateSchools,
name='PrivateSchools'),
]

The error occurs due to numerous views import not having a unique name and therefore only the last one counts.
when you do
from HomePage import views
from PublicSchools import views
from PrivateSchools import views
and then
path('HomePage/', views.HomePage),
you are effectively doing this
path('HomePage/', PrivateSchools.views.HomePage),
because PrivateSchools was imported as the last one.
Solve your problem by naming your views differently as for example
from HomePage import views as home_page_views
from PublicSchools import views as public_schools_views
from PrivateSchools import views as private_schools_views
and then for example
path('HomePage/', home_page_views.HomePage),
path('PublicSchools/', public_schools_views.PublicSchools),
path('PrivateSchools/', private_schools_views.PrivateSchools),

Related

My django project is not importing files from the same directory

from django.urls import path
from .entries import views
urlpatterns = [
path('', views.index),
path('add/', views.add)
]
This is my code..I am getting this error:
from .entries import views
ImportError: attempted relative import with no known parent
package
you need to name the imported views. try with:
from entries import views as entries_views
from django.urls import path
from entries import views
urlpatterns = [
path('', views.index),
path('add', views.add)
]
write like this
Your Mistack is : from **.**entries import views
remove this .(dot)
I hope this information help you

Django Tutorial Constant 404 error urlpattern recognition

I have searched endlessly for a solution and I can't seem to find it, even though I know this has been asked before. I am encountering a perpetual 404 error from the django tutorial. It appears to me that the 'urlpatterns' isn't recognizing another option of than admin.
backend/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('test/', include('meat.urls')),
path('admin/', include(admin.site.urls)),
]
backend/meat/urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
backend/meat/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(requests):
return HttpResponse('Hello Wold')
In order to access views inside the meat app.
you have included the urls of meat to the test/ path.
Thus you should use url /test/
if you are developing on local host then localhost:8000/test/
NB: I have tested this right now link
PS: try changing the path of admin site to path('admin/', admin.site.urls),

Django - importing view from Dependency

I'm trying to use this library since i want to add 2FA Auth to my project. In order to integrate the module in my project, i need to import their views to my urls.py file, right?
I tried to import SetupView, but i'm getting this error: module 'allauth_2fa.views' has no attribute 'homepage'. Here is what i understood: it looks like if i import a view from the dependency, it will only read those views from the dependency but not my own views declared on views.py.
from django.urls import path
from . import views
from django.conf.urls import url, include
from django.conf.urls import url
from allauth_2fa import views
app_name = "main"
urlpatterns = [
path("setup/", views.TwoFactorSetup.as_view(), name="setup"),
path("", views.homepage, name="homepage"),
path("register/", views.register, name="register"),
path("logout/", views.logout_request, name="logout"),
path("login/", views.login_request, name="login"),
]
Extra: SetupView will generate the page required to enable the 2FA authentication, that's why i need it. Later i will also import the other views required to have my two factor authentication fully running
At first you imported
from . import views
And then:
from allauth_2fa import views
And after that you tried to do:
path("", views.homepage, name="homepage"),
And views is allauth_2fa.views not from your project
So you just need to do like this:
from allauth_2fa import views as allauth_2fa_views
And then use it when you need

Why am I getting the error message ImportError: No module named <app>.urls?

I was recently building a django project. I have been looking through the files for a couple of hours now and can't find the problem that would result in this kind of error message. Below, I will show you all of the relevant files within the project.
base url.py:
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^display/', include('diplay.urls')),
]
app url.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
app views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<h2>HEY!</h2>")
I'm not sure why this is not working because I found a similar format online, and it seemed that every line was similar to the other one. When I try running the server, it gives me the error statement
ImportError: No module named diplay.urls
Any ideas?
1, make sure your app name is diplay which is same as in your base urls.py, I think maybe there is typo, should change to:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^display/', include('display.urls')),
]
2, make sure the file name of urls should be urls.py instead of url.py in both base and app folder
Does app diplay exists or is it typo of display
> diplay.urls
Also does urls file in app exists

Django: Support for string view arguments to url() is deprecated and will be removed in Django 1.10

New python/Django user (and indeed new to SO):
When trying to migrate my Django project, I get an error:
RemovedInDjango110Warning: Support for string view arguments to url() is deprecated
and will be removed in Django 1.10 (got main.views.home). Pass the callable instead.
url(r'^$', 'main.views.home')
Apparently the second argument can't be a string anymore. I came to create this code as it is through a tutorial at pluralsight.com that is teaching how to use Django with a previous version (I'm currently working with 1.9). The teacher instructs us to create urlpatterns in urls.py from the views we create in apps. He teaches us to create a urlpattern such as the following:
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', 'main.views.home')
]
to reference
def home(request):
return render(request, "main/home.html",
{'message': 'You\'ve met with a terrible fate, haven\'t you?'}) #this message calls HTML, not shown, not important for question
in the views.py of an app "main" that I created.
If this method is being deprecated, how do I pass the view argument not as a string? If I just remove the quotes, as shown in the documentation (https://docs.djangoproject.com/en/1.9/topics/http/urls/), I get an error:
NameError: name 'main' is not defined
I tried to "import" views or main using the code presented in this documentation:
from . import views
or
from . import main
which gave me:
ImportError: cannot import name 'views'
and
ImportError: cannot import name 'main'
I believe I've traced this down to an import error, and am currently researching that.
I have found the answer to my question. It was indeed an import error. For Django 1.10, you now have to import the app's view.py, and then pass the second argument of url() without quotes. Here is my code now in urls.py:
from django.conf.urls import url
from django.contrib import admin
import main.views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', main.views.home)
]
I did not change anything in the app or view.py files.
Props to #Rik Poggi for illustrating how to import in his answer to this question:
Django - Import views from separate apps
You should be able to use the following:
from django.conf.urls import url
from django.contrib import admin
from main import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home)
]
I'm not absolutely certain what your directory structure looks like, but using a relative import such as from . import X is for when the files are in the same folder as each other.
You can use your functions by importing all of them to list and added each one of them to urlpatterns.
from django.conf.urls import url
from django.contrib import admin
from main.views import(
home,
function2,
function3,
)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home/$', home),
url(r'function2/^$', function2),
url(r'^$', function3),
]

Categories

Resources