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
Related
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),
I am trying to import views from my apps into the urls.py file. For awhile, I was able to using "from app_name.views import view_name", but for some reason now it is not recognizing the app name.
I did not change anything in my settings.py file so I'm not sure what caused this. I did delete some migrations and an my database.
One thing that is weird is that I can import them if I go up a folder so, "from src.appname.views" and this seems to work, but is not what I want since by base directory is the src folder.
from django.contrib import admin
from django.urls import path
from pages.views import home_page, history, more_information, prop_analysis
from Product.views import property_analysis_tools, property_analysis_results
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_page),
path('home', home_page),
path('history', history),
path('information', more_information),
path('analyze', prop_analysis),
path('results', property_analysis_results)
Another odd thing is that when I run the server, all of these views seem to work. Can Django sometimes give off false errors?
In Django in order to import an app.view into project urls, we create an additional urls.py in every app and link that urls.py into
our project urls.py , just as shown below
change project urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls', namespace="pages")),
]
create another urls.py in your pages app
as below, similarly in any other apps.
from django.urls import path
from .views import (home_page,history,....)
app_name = 'pages'
urlpatterns = [
path(' ', homepage , name='homepage-view'),
path('history/',history, name='history-view'),
]
Every other view from respective app.views will be included in corresponding app.urls [if u have multiple apps]
nB: changes may occur according to your settings.py configurations,
for more, read this django URL documentation
watch Corey Schafer playlist best Django tutorial for beginners
I would like to know if there is a way to include only specific url endpoints in my Django urls.py.
Lets say i have a app called auth with this auth/urls.py
urlpatterns = [
url(r'^password/reset/$', PasswordResetView.as_view(),
name='rest_password_reset'),
url(r'^password/reset/confirm/$', PasswordResetConfirmView.as_view(),
name='rest_password_reset_confirm'),
url(r'^login/$', LoginView.as_view(), name='rest_login'),
url(r'^logout/$', LogoutView.as_view(), name='rest_logout'),
url(r'^user/$', UserDetailsView.as_view(), name='rest_user_details'),
url(r'^password/change/$', PasswordChangeView.as_view(),
name='rest_password_change'),
]
Now I have a urls.py like that:
urlpatterns = [
path('/', include('dj_rest_auth.urls'))
]
this includes all endpoints from auth/urls.py.
Is there a way to select (in urls.py) which URL to include? Lets say I only want login and logout to be included on my urls.py.
urlpatterns = [
path('/', include('dj_rest_auth.urls.rest_login')),
path('/', include('dj_rest_auth.urls.rest_logout'))
]
Something like that, how can I make it work?
I did this long before.
# inside view.py just create your own custom view
# rest and rest_auth import
from rest_auth.views import LoginView, LogoutView
class CustomLogoutView(LogoutView):
# yes u can keep it blank.
# over-riding just to distinguish from library views.
pass
Then inside any app's urls.py import required views and and pass then into path.
this way only required url will be exposed.
from .views import CustomLoginView
urlpatterns = [
path('login/', CustomLoginView.as_view(), name='login'),
]
Also you can directly import that views (not urls) which are included in library. and then create urls (path) from them.
I am doing django course from udemy, i did one experiment. Below is my folder structure
Project
appTwo
urls.py
ProTwo
urls.py
appTwo/urls.py
from django.conf.urls import url
from appTwo import views
urlpatterns = [
url(r'^$',views.help,name='help'),
url(r'^$',views.users,name='users'),
]
ProTwo/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from appTwo import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^help/',include('appTwo.urls')),
url(r'^users/',include('appTwo.urls')),
path('admin/', admin.site.urls),
]
Now when i try to open the page users by http://127.0.0.1:8000/users it opens the page help.html. For http://127.0.0.1:8000/help it opens help page. When I comment the first entry in urlpatterns in urls.py it opens the users page even if i try to open help page. Can anyone please guide me what wrong I am doing or its working as expected.
you need to use different patterns for each View:
urlpatterns = [
url(r'^help$', views.help, name='help'),
url(r'^users$', views.users, name='users'),
]
the name attribute is only useful for the concept of reversing, maybe further in your course?
also, the r'' strings in python are regular expressions, you might want to learn more about them.
I got the answer for this. Below two files need to be changed
appTwo/urls.py
from django.conf.urls import url
from appTwo import views
urlpatterns = [
url(r'^help$',views.help,name='help'),
url(r'^users$',views.users,name='users'),
]
ProTwo/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from appTwo import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^appTwo/',include('appTwo.urls')),
path('admin/', admin.site.urls),
]
Explanation:
When I type in browser "basic url" i mean the address here it is http://127.0.0.1:8000/, it will go to ProTwo/urls.py which is the project folder. This will open the index page as per line url(r'^$',views.index,name='index'). If you need two configure two different page, give url(r'^appTwo/',include('appTwo.urls')), in the ProTwo/urls.py. This will call appTwo/urls.py. Now for help type http://127.0.0.1:8000/appTwo/help and for users type http://127.0.0.1:8000/appTwo/users.
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),
]