Django admin site URL routing issues/inconsistencies - python

I am working on a website on Django 1.10, and I am trying to set up my admin site so the URL is at "/pcari/admin" instead of "/admin"
Here is my root urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
# Admin site
url(r'^admin/', admin.site.urls),
# Regular site
url(r'^pcari/', include('pcari.urls')),
]
Here is my app urls.py:
from django.conf.urls import url
from django.contrib import admin
from . import views
app_name = 'pcari'
urlpatterns = [
# Admin site
url(r'^admin/', admin.site.urls),
# User-facing views
url(r'^$', views.index, name='index'),
...
]
This setup technically works in that if I visit "127.0.0.1:8000/pcari/admin" I get the admin site, but I also get the admin site if I visit "127.0.0.1:8000/admin", which is something I do not want.
However, if I remove the "url(r'^admin/', admin.site.urls)," line from the root urls.py file, I get a weird error when I try to access "127.0.0.1:8000/pcari/admin":
NoReverseMatch at /pcari/admin/
u'admin' is not a registered namespace
Request Method: GET
Request URL: http://127.0.0.1:8000/pcari/admin/
Django Version: 1.10.5
Exception Type: NoReverseMatch
Exception Value:

in your main urls.py
urlpatterns = [
# Admin site
url(r'^pcari/admin/', admin.site.urls),
# Regular site
url(r'^pcari/', include('pcari.urls')),
]
and remove the admin url from the app urls

However, if I remove the "url(r'^admin/', admin.site.urls)," line from the root urls.py file, I get a weird error when I try to access "127.0.0.1:8000/pcari/admin":
NoReverseMatch at /pcari/admin/
u'admin' is not a registered namespace
Request Method: GET
Request URL: http://127.0.0.1:8000/pcari/admin/
Django Version: 1.10.5
Exception Type: NoReverseMatch
Exception Value:
Reason:
When you remove url(r'^admin/', admin.site.urls), from main urls.py that means you don't have any admin namespace becuase this line registered "admin" namespace that is define in django code. https://github.com/django/django/blob/master/django/contrib/admin/sites.py#L279
this line show django admin will register as "admin" namespace
When you removed and added admin site in "pcari" app that means this app is register namespace "pcari". But in django admin template has hardcode url in template like {% url admin:index %} that gives NoReverseMatch Exception
https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/base.html#L44 there is hardcode admin namespace in django templates that cause error
EDIT:
You added in app urls
app_name = 'pcari'
That's creating this Noreverse Problem. Remove this app name line. Because it will work as pacri namespace if you add this.

Related

Page not found at /polls

I am a total beginner in "django" so I'm following some tutorials currently I' am watching https://youtu.be/JT80XhYJdBw Clever Programmer's tutorial which he follows django tutorial
Everything was cool until making a polls url
Code of views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
HttpResponse("Hello World.You're at the polls index")
Code of polls\urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Code of Mypr\urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/',admin.site.urls),
path('polls/',include('polls.urls')),
]
I don't get it I did the same thing but I'm getting error not only polls.In one turtorial he decided to make blog,and again the same error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in Mypr.urls, Django tried these URL patterns, in
this order:
admin/
The current path, polls/, didn't match any of these.
Please my seniors help me.
Note:I'm using the latest versions of django,windows and as editor I'm using Pycharm.
Already tried(and did not work):
from django.urls import path
from polls.views import index
urlpatterns = [
path('', index, name='index'),
]
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in Mypr.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file.
Change that to False, and Django will display a standard 404 page.
try to access polls/ URL in your browser then you will access the page
Because you have accessed the URL of your project, you have to go to this URL to access your app
try by changing your code like this
from django.urls import path
from polls.views import index
urlpatterns = [
path('', index, name='index'),
]
I was facing the same error. Feel kinda dumb after figuring out how to solve this.
Your urls.py content is all right
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
But it must be included in the mysite\mysite\urls.py and not in mysite\urls.py.
That means the inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it.

Getting error when I am trying to fetch my main url patterns

I am a beginner in Django I have created a Django project in that I have included two more application when I did add urls.py file in both Applications both are working well but when I am fetching my main admin URL it is giving an error
Page not found (404)
Request Method: GET
URL: http://127.0.0.1:8000/
the URLconf defined in mac.urls, Django tried these URL patterns, in this order:
admin/
shop/
blog/
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to
False, and Django will display a standard 404 page.
when I am fetching this URL in http://127.0.0.1:8000/ i am getting an error it is working for http://127.0.0.1:8000/shop/
here is my main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls')),
path('blog/', include('blog.urls')),
]
Your django app has 3 routes:
http://127.0.0.1:8000/admin/ goes to django admin app
http://127.0.0.1:8000/shop/ goes to your shop app
http://127.0.0.1:8000/blog/ goes to your blog app
And since you have no configuration for http://127.0.0.1:8000, you see an error instead.
You can see that in the error, when django tries to match your url with list of available urls.
If you want to get admin app on url http://127.0.0.1:8000, change urls.py to:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', admin.site.urls),
path('shop/', include('shop.urls')),
path('blog/', include('blog.urls')),
]
It's generally not advisable to set admin app at root url - it has it's own system of urls inside (admin/<app_name>/<model_name>), so chances are it will shadow your urls and make the unreachable.
Create a view that will be your front page.
From there you should link to the other areas of your website.
Don't direct that to admin, that's ridiculous.
You've created a Django project and started two apps. You should have a project-level urls.py file and then an app-level urls.py file for each of your apps.
To explain that in greater detail lets say our Django project is called config and our two apps are called app1 and app2. Your project-level urls.py file, which will be located at config/urls.py, could contain the following:
# config/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name='home.html'),
name='home'),
path('app1/', include('app1.urls')),
path('app2/', include('app2.urls')),
]
In this file we specify a route for our admin panel, which on your local server will be located at http://127.0.0.1:8000/admin. We've also specified a home route, the second path with the empty string. This means when you navigate to http://127.0.0.1:8000/ you will be directed to your home page (for the example above I just used a generic built-in view). It's not a good idea to route immediately to your admin panel.
We've also included paths to our other two apps. What these two lines essentially say is: "include the urls from this other app". Now we need to create two urls.py files, one for each of our apps. In this example I'll just focus on the urls.py file for app1:
# app1/urls.py
from django.urls import path
from .views import AppContentView
urlpatterns = [
path('content/', AppContentView.as_view(),
name='app_content'),
]
This is a view that you would have to create, but what we've now done is we've created one path that will be located at http://127.0.0.1:8000/app1/content. In fact any new paths we create in this file will always begin with http://127.0.0.1:8000/app1/, because we've already told Django in our project-level urls.py to include the urls from the app1 urls.py file so we've essentially prefixed all of these paths with /app1/.
If you think of urls configurations like a tree it might help too:
Project Level Url Configs.
|
|
|
___________________________
| |
| |
App 1 Url Configs. App 2 Url Configs.

How to fix url in python custom login

I have 2 django app. user and management. I have added dashboard/ in main app urls which redirects to user app urls. For this we need to login. All urls in user app is: dashboard/ and for login dashboard/login
but it shows error in /login.
ERROR:
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/login
Main app urls.py
from user import urls as user_urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('dashboard/', include(user_urls))
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
User app urls.py
from django.urls import path, include
from nh_user import views as users_views
from management import urls as management_urls
urlpatterns = [
path('login', users_views.user_login, name='user-login'),
path('logout', users_views.user_logout, name='user-logout'),
path('dashboard/', include(management_urls)),
path('', users_views.index, name='user-index'),
]
You specified dashboard/ as the route for all the urls in your users app. Django won't look for these urls unless you include '/dashboard/' in your address bar.
Currently the working path is http://localhost:8000/dashboard/login.
If you want to change your login path to http://localhost:8000/login you need to remove 'dashboard/' as the route in your project urls.py.
from user import urls as user_urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(user_urls))
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
All the routes for the urls in your users app will now be at directly at http://localhost:8000/route-name without anything preceding them.

Django can't find social auth url pattern

I have tried to set up social authentication for steam login button. I have followed all these steps, and added steam necessary stuff: adding API key to settings and steam.SteamOpenId to authentication backends.
The error:
Using the URLconf defined in Test.urls, Django tried these URL patterns, in this order:
^ ^$ [name='index']
^login/(?P<backend>[^/]+)/$ [name='begin']
^complete/(?P<backend>[^/]+)/$ [name='complete']
^disconnect/(?P<backend>[^/]+)/$ [name='disconnect']
^disconnect/(?P<backend>[^/]+)/(?P<association_id>[^/]+)/$ [name='disconnect_individual']
^admin/
Hyperlink i used to make login button:
Login
urls.py file of project:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^', include('Home.urls')),
url('', include('social.apps.django_app.urls', namespace='social')),
url(r'^admin/', admin.site.urls),
]
urls.py file of application Home:
from django.conf.urls import url
from . import views
app_name = 'Home'
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Full Project Here.
I am wondering if there is still something that i need to set up, do i need to add views to url patterns? If so, then which? where is social authentication urls file located?

Django URLconf error

I am trying to build a website with Django. I am new to Django and have followed the Tango with Django tutorial.
I keep getting an URlconf error I do not understand.
I have a domain (www.example.com), an app (mainApp) and two views in mainApp (homePage, registration).
I want
http://www.example.com to be matched with the homePage view
http://www.example.com/registration/ to be matched with the registration view.
My urls.py file for the project is
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns(
'',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('mainApp.urls')),
)
The urls.py file for mainApp is
from django.conf.urls import patterns, url
from mainApp import views
urlpatterns = patterns(
'',
url(r'^$', views.homePage, name='homePage'),
url(r'^registration/$', views.registration, name='registration'),
)
This configuration displays the homePage view correctly, but not the registration view. The error is:
Using the URLconf defined in myProject.urls, Django tried these URL
patterns, in this order:
^admin/
^$
^/
The current URL, registration/, didn't match any of these.
What causes the error?
It looks fine. If you're hosting it online you should restart the webapp, if hosting it locally then restart the local host. Without restarting it the changes don't register.

Categories

Resources