Django Project: namespace 'admin' isn't unique - django

on trying to run C:\Python34/python manage.py makemigrations, I get the following error: (following a tutorial from www.testandtrack.io)
Error
WARNINGS: ?: (urls.w005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLS in this namespace
What precisely do I need to change and where do I need to look?
teachers/url.py
from django.contrib import admin
from django.urls import path
from django.urls import include, path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.teachers, name='teachers'),
]
url.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls')),
path('teachers/', include('teachers.urls')),
]
main/url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('header/', views.header, name='header'),
path('', views.index, name='index'),
]
I've pasted the various url.py files above and imagine it's a problem somewhere there. could anyone please point me in the right direction, with an explanation please?
I've considered that I could/should remove
path('admin/', admin.site.urls),
from all but the urls.py file (root) .....when I do remove this, I don't get the same error, but I don't know if that will cause other problems and if this is the right thing to do?

You are declaring
path('admin/', admin.site.urls),
three times in your urls files. You just have to declare it once in the root urls.py of your project.

You are correct in that the error stems from repeating path('admin/', admin.site.urls), in all of your url.py files. It is normally only declared at the root level as others have pointed out.
Think of it like this - You wouldn't want to have a separate admin interface for each app, rather you would want to be able to manage all of your apps from one admin interface which is exactly what occurs when you have it only in the root urls.py file.
Also, although the apps should be modular and independent they still need to be connected to a project to work.

Set your main/urls.py as
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path(
"header/",
TemplateView.as_view(template_name="header.html")),
]
And fix this inside the main URLs file teachers/urls.py as follows:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
urlpatterns = [
path(
'admin/',
admin.site.urls),
path('',
include('main.urls')),
]

it's because of multiple declarations of that admin path. remove those extra line path('admin/', admin.site.urls) line from urls.py file except the project's url.py

There is a repetition of an admin path
Search in the files urls.
and delete duplicate path

Related

Base "/" not found in Django

My django project ornaments has only one app called balls. I would like the server to point to its index when I do manage.py runserver but I get this error:
Here's my urls.py:
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('balls/', include('balls.urls')),
path('admin/', admin.site.urls),
]
urlpatterns += [
path('', RedirectView.as_view(url='balls/', permanent=True)),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I am also including the project structure. Please note that the string ball appears nowhere in my code (as per a search).
According to your screenshot, you're sending requets to http://127.0.0.1:8000/ball and this URL not found on given path path('balls/', include('balls.urls')) so try to go to this http://127.0.0.1:8000/balls

How do you implement Django namespaces

I have started my journey with Django and I am thoroughly loving it. I do have a question about namespaces which I hope someone will be able to help me with and explain why it works like that.
I understand namespaces are used to make sure that if you have two pages with the same name that the url and reverse function points to the right page. I have been trying to implement namespaces in a test app I am writing but have not been able to do it.
Here is what I have so far (This is without namespaces as I haven't been able to get it to work.
Extract from app urls.py
from django.urls import path, re_path
from . import views
urlpatterns = [
path('', views.index, name = "index"),
]
project urls.py
from django.contrib import admin
from django.urls import path, include, re_path
import gallery
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('index/', include('gallery.urls')),
]
And lastly, this is my view.py file in my app folder
from django.shortcuts import render
from django.urls import reverse
# Create your views here.
def index(request):
return render(request,"gallery/index.html")
Any help will be appreciated
URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It’s a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed. In other words, since multiple instances of a single application will share named URLs, namespaces provide a way to tell these named URLs apart. See URL namespaces
In your case:
project urls.py
from django.contrib import admin
from django.urls import path, include, re_path
import gallery
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls', namespace='your-namespace')),
path('index/', include('gallery.urls',namespace='your-namespace')),
]
Extract from app urls.py
from django.urls import path, re_path
from . import views
app_name = 'app'
urlpatterns = [
path('', views.index, name = "index"),
]
in the template:
{% url 'app:index' %}

How to remove the unspecified space between root and include url in django 2.2

I am trying to create a Django website. I have specified the path to one of my apps. But I am getting an unspecified space in the pattern.I am using Django 2.2. screenshot of the django debug page
my base urls.py file
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('homepage.urls')),
path('account/', include('account.urls')),
path('admin/', admin.site.urls),
path('db/', include('dashboard.urls')),
]
my dashboard/urls.py file
from django.urls import include, path
from .import views
urlpatterns = [
#author dashboard
path('authordboard/<int:auth_id/',views.author_dboard, name='author_details'),
#reviewer dashboard
path('reviewdboard/<int:re_id/',views.re_dboard, name='reviewer_details'),
]
I have included the dashboard app in the settings.py file.
Please help! Thanks in advance!
you must close variable declared in url. in your dashboard/urls.py:
from django.urls import include, path
from .import views
urlpatterns = [
#author dashboard
path('authordboard/<int:auth_id>/',views.author_dboard, name='author_details'),
#reviewer dashboard
path('reviewdboard/<int:re_id>/',views.re_dboard, name='reviewer_details'),
]

How to fix the error for django 'django.core.exceptions.ImproperlyConfigured' with urls?

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
There is an error when i add url to url.py.
when i run the code in terminal : 'python manage.py runserver' ; then the follwing error is displayed in the terminal -
django.core.exceptions.ImproperlyConfigured: The included URLconf
'<module 'polls.urls' from
'C:\\Users\\Administrator\\PycharmProjects\\website2\\mysite\\polls\\urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
I searched everywhere for the solution but i couldn't find it. Please help me to get out of it.
I think this error most of u misunderstand, circular error imports are very rare in django only in flask it is frequent .
This error is caused by mispelling 'urlpatterns' in the urls.py which has been created in a django app.
Also it can be caused by not defining it in your urlpatterns in the urls.py file
Probably the error is caused by one of the above.
Its that easy
make sure in your polls app, you have a file called urls.py, which should look something like this:
from django.urls import path
from . import views
urlpatterns = [
path('', views.your_view),
]
If you haven't configured the views.py page in your polls app, then you can leave urlpatterns blank for now and you shouldn't see any errors.
This error might be coming because of the adimn in urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
When the polls path was removed by me, the website was running properly. So try comment the polls path in urls
urlpatterns = [
path('admin/', admin.site.urls),
#path('polls/', include('polls.urls')),
]

urls.py django 2.0.2 need parameters

i'm learning django and i'm making a blog tutorial but it's from older version that i have, i have version 2.0.2 and i'm not understand documentation my problem is that i dont know how configure my urls.py
this is my three proyect:
i need to put archive.html in 127.0.0.1:8000/ and this is my urls code
codigofacilito/blog.urls.py :
"""codigofacilito URL Configuration
The urlpatterns list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin<br/>
from django.urls import path<br/>
from . import views<br/>
urlpatterns = [
path('admin/', admin.site.urls),
]
and codigofacilito/codigofacilito.urls.py:
from django.contrib import admin
from django.urls import path
from . import views
enter code here`urlpatterns = [
path('admin/', admin.site.urls),
]
from django.views.generic import TemplateView
urlpatterns = [
path('', TemplateView.as_view(template_name = 'archive.html')),
path('admin/', admin.site.urls),
]
put this in your project's urls.py, this will directly render the archive.html when you visit 127.0.0.1:8000/ in your browser.
but if you want to render data from backend in this html page then i suggest you to use views (function based , class based, etc..)
then you just have to import the views in url file and specify path for that.
from your_app.views import your_view
urlpatterns = [
path('/', your_view),
]