python - Error with two same views in different apps in django project -
working on django project. there 2 apps in project 1 custom defined "admin", , "home". using views same in both app eg:- admin app urls.py file is
from django.conf.urls import url . import views urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^logout$', views.logout, name='logout'), url(r'^dashboard$', views.dashboard, name='dashboard'), url(r'^profile$', views.profile, name='profile'), url(r'^edit-profile$', views.edit_profile, name='edit-profile'), url(r'^check-password$', views.check_password, name='check-password'), url(r'^testing$', views.testing_database, name='testing'), ]
and home's app urls.py file is:-
from django.conf.urls import url . import views urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^sport$', views.sport, name='sport'), url(r'^register$', views.signup_signin, name='register'), url(r'^logout$', views.logout_view, name='logout'), url(r'^dashboard$', views.dashboard, name='dashboard'), url(r'^create-new-event$', views.create_new_event, name='create-new-event') ]
so can check views same in both of views.py file, why whenever trying login admin panel it's redirectingme home app's dashboard, , when try logout admin panel it's redirecting me on home's app index page. think problem of same views in both apps. tried import views of particular app in respective urls.py file. example:-
from home import views
in home's urls.py file , admin
from admin import views
but giving me error:-
from admin import views importerror: cannot import name views
my root urls.py file :-
from django.conf.urls import include, url django.contrib import admin urlpatterns = [ url(r'^admin/', include('admin.urls')), url(r'^home/', include('home.urls')), ]
i checked solution on stackoverflow did not solution
http://stackoverflow.com/questions/26083515/same-functions-in-different-views-django
okay django has solve problem of yours.
what can specify namespace
urls in root urls.py , when using can use namespace
value while reversing url in html templates when specify urls redirect to.
something this:
root urls.py pretty same yours, add namespace
value home.urls
this:
url(r'^admin/', include('admin.urls', namespace='home')),
so whenever using home app urls, can redirecting:
<a href="{% url "home:logout" %}> logout </a>
so when click on <a>
tag of logout, home namespace , hence pick url name
logout
home app , not admin app irrespective of order in have included urls in root url.py
there other solutions require changing url patterns. like, either can change original url value or can have different name
value of same urls in different apps, when django providing flexibility want using power of namespace
.
see -> https://docs.djangoproject.com/en/1.10/topics/http/urls/#url-namespaces-and-included-urlconfs
i hope above resolve issue.
Comments
Post a Comment