Wildcard url in Django using re_path - django

As per Django documentation ,below url pattern will work for
http://127.0.0.1:8000/index . But I want also to make it work for
http://127.0.0.1:8000/api/index/
http://127.0.0.1:8000/api/json/index/
from django.urls import include, re_path
urlpatterns = [
re_path(r'^index/$', views.index, name='index'),
...
]
How we can achieve it using re_path

you can add multiple entries of re_path with different urls endpoints for same views.index like this .
urlpatterns = [
re_path(r'^index/$', views.index, name='index'),
re_path(r'^api/json/index/$', views.index, name='index'),
re_path(r'^api/index/$', views.index, name='index'),
...
]

Related

Why is the route correct but still giving 404 error on Django

I got a 404 error on my website when accessing the correct route, did I do something wrong
urls.py handles the main route
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'api/facebook/(.+?)', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
urls.py handles routes in the apifacebooks app
from django.urls import path
from . import views
urlpatterns = [
path('api/facebook/like-post',views.like_post)
]
And when I go to http://localhost:8000/api/facebook/like-post I get a 404 error
Image error 404
My question has been solved, thanks
In your apifacebooks app change the path because you had "api..." double in the path
from django.urls import path
from . import views
urlpatterns = [
path('like-post/',views.like_post)
]
And in the root urls.py just
path('api/facebook/', ....)
In your code, the url pattern would be "http://localhost:8000/api/facebook/api/facebook/like-post".
As explained id Django docs:
Whenever Django encounters include(), it chops off whatever part of
the URL matched up to that point and sends the remaining string to the
included URLconf for further processing.
Remove "api/facebook/" in "apifacebooks.urls", for example:
Main urls.py
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'api/facebook/(.+?)/', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
apifacebooks.urls
from django.urls import path
from . import views
urlpatterns = [
path('like-post',views.like_post)
]
Or you can try to place remove "r'api/facebook/(.+?)'", for example:
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]

Django Post detail url url not found

Django Post list/detail urls, domain.com/api/v1/1 and domain.com/api/v1 not found given the following url patterns, note that pk with value of 1 exists,
Project urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include('posts.urls'))
]
Api urls
from django.urls import path
from .views import PostList, PostDetail
urlpatterns = [
path('<int:pk>/', PostDetail.as_view()),
path('', PostList.as_view()),
]
Try adding a slash to the end of api/v1
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include('posts.urls'))
]

django.urls.exceptions.NoReverseMatch: 'admin' is not a registered namespace

I have a Django projects with two apps, "projects" and "codebox", they were running fine, then at some point I got the following error:
django.urls.exceptions.NoReverseMatch: 'admin' is not a registered
namespace
If I remove the link to my admin panel from my template this error goes away, but then I can't get to my admin panel:
Admin
I was working in the urls.py files when this error occurred, have I changed something that is inadvertently having an impact on the admin link?
Here is my top level urls.py file:
from django.contrib import admin
from django.urls import include, path, re_path
urlpatterns = [
# path('', include('projects.urls')),
path('projects/', include('projects.urls')),
re_path('^accounts/', include('django.contrib.auth.urls')),
re_path('^logged_out/', include('projects.urls')),
path('codebox/', include('codebox.urls')),
]
Here is my urls.py for "projects":
from django.urls import path, include
from . import views
app_name = 'projects'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('insight/', views.InsightView.as_view(), name='insight'),
path('logged_out/', views.LoggedoutView.as_view(), name='loggedout'),
path('insight/subgen/', views.SubgenView.as_view(), name='subgen'),
]
And here is urls.py for my second app, codebox:
from django.urls import path, include
from . import views
app_name = 'codebox'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('form/', views.CreateView, name="form"),
]
add this in top of project/urls.py
from django.conf.urls import include, url
then add this in the url_pattern
url(r' ', include(('<app_name>.urls','<app_name>'), namespace='<app_name>')),
hope this will work
Check your base html file which you're extending to other html pages. If there is some space between urls tag remove that space and save your file and try again you can check below code for ref.
from django.contrib import admin
from django.urls import path, include
from passapp import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('passapp/', include('passapp.urls')),
]
href="{% url 'admin:index' %}"

Error W005 URL namespace isn't unique

'Copied this code from the Django tutorial into my app's urls.py file...
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
When I start my server it produces the following error...
(urls.W005) URL namespace 'polls' isn't unique. You may not be able
to reverse all URLs in this namespace
I've tried using some other name besides 'polls' but with the same result. What am I doing wrong?
Check your root urls file, and be sure to have unique name:
(django 2+)
For example:
mysite/urls.py
-->
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('polls.urls')), #polls.urls is unique
path('admin/', admin.site.urls), #admin.site.urls is unique
]
from django.conf.urls import url
import views
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^(?P<question_id>[0-9a-f-]+)/$',views.detail,name='detail'),
url(r'^(?P<question_id>[0-9a-f-]+)/results/$',views.results,name='results'),
url(r'^(?P<question_id>[0-9a-f-]+)/vote/$',views.vote,name='vote'),
]
Write your url file like shown above.

What's the meaning of using patterns in Django URLs?

In my Django project, I find the project urls.py resolve URLs directly
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^test/', include('test.urls')),
]
but I find the app urls.py solution always use
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^new$', views.new, name='new'),
)
when I try to change app's urls.py to
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^new$', views.new, name='new'),
]
or
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
urlpatterns += patterns('',
url(r'^new$', views.new, name='new'),
)
also works, so I want to know the meaning of using patterns and which one is better.
Patterns is deprecated since 1.8 (and removed in 1.10)
from the 1.8 docs:
Deprecated since version 1.8:
urlpatterns should be a plain list of django.conf.urls.url() instances instead.